Page 1 of 1
How to tell which key was pressed?
Posted: Wed 08 May 2013, 15:46
by R-S-H
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
Posted: Wed 08 May 2013, 16:54
by Karl Godt
No straight idea .
evbug.ko kernel module if enabled, would show .
xev is another small program to show keys .
Posted: Wed 08 May 2013, 17:01
by Flash
Yes, xev has worked for me.
R-S-H, you might find the answer toward the end of
this thread.
Here, maybe.
Posted: Wed 08 May 2013, 17:29
by R-S-H
Hi,
thanks for the posts and the links.
I would like to try
Xev but could not find it.
Any link to download
Xev?
RSH
Edit:
Found it: it's in Puppy!
Posted: Wed 08 May 2013, 17:37
by L18L
R-S-H wrote:I would like to try Xev but could not find it.
And what about
Posted: Wed 08 May 2013, 17:56
by R-S-H
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:
Code: Select all
MyKey=$(HereAFunctionThatReturnsAKeyCode)
if [ "$MyKey" = "MyKeyDefinitionHere" ]; then
...
...
or
Code: Select all
MyKey=$(`HereAFunctionThatReturnsAKeyCode`)
if [ "$MyKey" = "MyKeyDefinitionHere" ]; then
...
...
or
Code: Select all
MyKey=`HereAFunctionThatReturnsAKeyCode`
if [ "$MyKey" = "MyKeyDefinitionHere" ]; then
...
...
etc.pp
Thanks,
RSH
Posted: Wed 08 May 2013, 19:03
by Keef
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.
Posted: Wed 08 May 2013, 19:48
by SFR
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).
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
Greetings!
Posted: Wed 08 May 2013, 20:40
by R-S-H
Hi.
SFR wrote:it detects keystrokes globally
That's exactly how it would used to be!
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
Posted: Wed 08 May 2013, 23:16
by muggins
I wonder if this could be modified to suit your needs?
http://ldp.linux.no/LDP/abs/html/abs-guide.html#BASHEK
Posted: Thu 09 May 2013, 02:43
by Flash
That's a big page which takes a while to stabilize at the relevant spot, so be patient and wait for 10 or 20 seconds.