Author |
Message |
R-S-H
Joined: 18 Feb 2013 Posts: 490
|
Posted: Wed 08 May 2013, 11:46 Post subject:
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
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 4208 Location: Kiel,Germany
|
Posted: Wed 08 May 2013, 12:54 Post subject:
|
|
No straight idea .
evbug.ko kernel module if enabled, would show .
xev is another small program to show keys .
|
Back to top
|
|
 |
Flash
Official Dog Handler

Joined: 04 May 2005 Posts: 12813 Location: Arizona USA
|
Posted: Wed 08 May 2013, 13:01 Post subject:
|
|
Yes, xev has worked for me.
R-S-H, you might find the answer toward the end of this thread. Here, maybe.
|
Back to top
|
|
 |
R-S-H
Joined: 18 Feb 2013 Posts: 490
|
Posted: Wed 08 May 2013, 13:29 Post subject:
|
|
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!
_________________ LazY Puppy Home
The new LazY Puppy Information Centre
|
Back to top
|
|
 |
L18L
Joined: 19 Jun 2010 Posts: 3431 Location: www.eussenheim.de/
|
Posted: Wed 08 May 2013, 13:37 Post subject:
|
|
R-S-H wrote: | I would like to try Xev but could not find it. | And what about
|
Back to top
|
|
 |
R-S-H
Joined: 18 Feb 2013 Posts: 490
|
Posted: Wed 08 May 2013, 13:56 Post subject:
|
|
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: | MyKey=$(HereAFunctionThatReturnsAKeyCode)
if [ "$MyKey" = "MyKeyDefinitionHere" ]; then
...
...
|
or
Code: | MyKey=$(`HereAFunctionThatReturnsAKeyCode`)
if [ "$MyKey" = "MyKeyDefinitionHere" ]; then
...
...
|
or
Code: | MyKey=`HereAFunctionThatReturnsAKeyCode`
if [ "$MyKey" = "MyKeyDefinitionHere" ]; then
...
...
|
etc.pp
Thanks,
RSH
_________________ LazY Puppy Home
The new LazY Puppy Information Centre
|
Back to top
|
|
 |
Keef

Joined: 20 Dec 2007 Posts: 888 Location: Staffordshire
|
Posted: Wed 08 May 2013, 15:03 Post subject:
|
|
I suppose you could pipe the results of xev to a text file, grep the results. then put that into a variable.
Code: | xev | cat > keypress |
Then again, I don't know how to get rid of the xev window, which would be a pain.
|
Back to top
|
|
 |
SFR

Joined: 26 Oct 2011 Posts: 1655
|
Posted: Wed 08 May 2013, 15:48 Post subject:
|
|
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: | #!/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!
_________________ [O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
|
Back to top
|
|
 |
R-S-H
Joined: 18 Feb 2013 Posts: 490
|
Posted: Wed 08 May 2013, 16:40 Post subject:
|
|
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
_________________ LazY Puppy Home
The new LazY Puppy Information Centre
|
Back to top
|
|
 |
muggins
Joined: 20 Jan 2006 Posts: 6747 Location: hobart
|
Posted: Wed 08 May 2013, 19:16 Post subject:
|
|
I wonder if this could be modified to suit your needs?
http://ldp.linux.no/LDP/abs/html/abs-guide.html#BASHEK
|
Back to top
|
|
 |
Flash
Official Dog Handler

Joined: 04 May 2005 Posts: 12813 Location: Arizona USA
|
Posted: Wed 08 May 2013, 22:43 Post subject:
|
|
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.
|
Back to top
|
|
 |
|