How to tell which key was pressed?
How to tell which key was pressed?
Hi,
how can I check and get the result if any key like Strg, Alt or Shift is pressed?
I want to use/do this in bash.
Thanks
RSH
how can I check and get the result if any key like Strg, Alt or Shift is pressed?
I want to use/do this in bash.
Thanks
RSH
- L18L
- Posts: 3479
- Joined: Sat 19 Jun 2010, 18:56
- Location: www.eussenheim.de/
And what aboutR-S-H wrote:I would like to try Xev but could not find it.
Code: Select all
xev
Ok,
as I've edited in my previous post, I've found it in the OS. But it seems not to be useful.
I need to get the code from pressed key to be submitted by a variable. To have a window opened and to watch the results in the terminal is not really useful to my purposes.
I'm looking for a solution like this:
or
or
etc.pp
Thanks,
RSH
as I've edited in my previous post, I've found it in the OS. But it seems not to be useful.
I need to get the code from pressed key to be submitted by a variable. To have a window opened and to watch the results in the terminal is not really useful to my purposes.
I'm looking for a solution like this:
Code: Select all
MyKey=$(HereAFunctionThatReturnsAKeyCode)
if [ "$MyKey" = "MyKeyDefinitionHere" ]; then
...
...
Code: Select all
MyKey=$(`HereAFunctionThatReturnsAKeyCode`)
if [ "$MyKey" = "MyKeyDefinitionHere" ]; then
...
...
Code: Select all
MyKey=`HereAFunctionThatReturnsAKeyCode`
if [ "$MyKey" = "MyKeyDefinitionHere" ]; then
...
...
Thanks,
RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]
The new LazY Puppy Information Centre[/url][/b]
I suppose you could pipe the results of xev to a text file, grep the results. then put that into a variable.
Then again, I don't know how to get rid of the xev window, which would be a pain.
Code: Select all
xev | cat > keypress
It's rather ugly low-level trick, but should (more or less) do what you want.
Anyway, I can't come up with nothing better.
The main downside (or advantage) of this method is that it detects keystrokes globally, regardless of which window/application is currently in use (i.e. focused).
Greetings!
Anyway, I can't come up with nothing better.
The main downside (or advantage) of this method is that it detects keystrokes globally, regardless of which window/application is currently in use (i.e. focused).
Code: Select all
#!/bin/bash
# In Puppy it seems to be always /event0, but in other distros it may vary.
KEYBOARD="/dev/input/event0"
checkkey() {
dd if="$KEYBOARD" bs=16 count=1 2>/dev/null | hexdump | cut -f8 -d ' ' | head -1
}
# -----------------------------------------------------------------------------
# Just an example how to use the above function:
echo "Press q to quit"
while true; do
sleep 0.1 # <- to prevent oversensitivity
KEY=$(checkkey)
case $KEY in
002a ) echo "Left Shift" ;;
001d ) echo "Left Control" ;;
0036 ) echo "Right Shift" ;;
009d ) echo "Right Control" ;;
0038 ) echo "Left Alt" ;;
00b8 ) echo "Right Alt (Alt-Gr)" ;;
0010 ) echo -e "\n'q' has been pressed, exiting..." && break ;;
* ) echo " (other key...)" ;;
esac
done
read -t0.1 -N 255 KEY # Empty keyboard buffer, to avoid junk on exit
exit
# -----------------------------------------------------------------------------
# PS. To get hex codes of other keys, launch this in terminal:
while true; do dd if=/dev/input/event0 bs=16 count=1 2>/dev/null | hexdump | cut -f8 -d ' ' | head -1; sleep 0.1; done
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]
[b][color=green]Omnia mea mecum porto.[/color][/b]
Hi.
After you doing the basic script for the LazY MAID as well as your work done on the variomenu (der-schutzhund) again a nice working solution/script. You're a real coder with rich knowledge!
How do you say, in English: Hat's up (right? - in German this would be: Hut ab!)!
Thank you very much, SFR!
RSH
That's exactly how it would used to be!SFR wrote:it detects keystrokes globally
After you doing the basic script for the LazY MAID as well as your work done on the variomenu (der-schutzhund) again a nice working solution/script. You're a real coder with rich knowledge!
How do you say, in English: Hat's up (right? - in German this would be: Hut ab!)!
Thank you very much, SFR!
RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]
The new LazY Puppy Information Centre[/url][/b]
I wonder if this could be modified to suit your needs?
http://ldp.linux.no/LDP/abs/html/abs-guide.html#BASHEK
http://ldp.linux.no/LDP/abs/html/abs-guide.html#BASHEK