How to tell which key was pressed?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
R-S-H
Posts: 487
Joined: Mon 18 Feb 2013, 12:47

How to tell which key was pressed?

#1 Post 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

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#2 Post by Karl Godt »

No straight idea .

evbug.ko kernel module if enabled, would show .

xev is another small program to show keys .

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#3 Post by Flash »

Yes, xev has worked for me. :)

R-S-H, you might find the answer toward the end of this thread. Here, maybe.

R-S-H
Posts: 487
Joined: Mon 18 Feb 2013, 12:47

#4 Post 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! :lol:
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#5 Post by L18L »

R-S-H wrote:I would like to try Xev but could not find it.
And what about

Code: Select all

xev
:?:

R-S-H
Posts: 487
Joined: Mon 18 Feb 2013, 12:47

#6 Post 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
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#7 Post by Keef »

I suppose you could pipe the results of xev to a text file, grep the results. then put that into a variable.

Code: Select all

xev | cat > keypress
Then again, I don't know how to get rid of the xev window, which would be a pain.

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#8 Post 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. :wink:

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!
[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]

R-S-H
Posts: 487
Joined: Mon 18 Feb 2013, 12:47

#9 Post by R-S-H »

Hi.
SFR wrote:it detects keystrokes globally
That's exactly how it would used to be! :D

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! :D

RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy Home
The new LazY Puppy Information Centre[/url][/b]

muggins
Posts: 6724
Joined: Fri 20 Jan 2006, 10:44
Location: hobart

#10 Post by muggins »

I wonder if this could be modified to suit your needs?

http://ldp.linux.no/LDP/abs/html/abs-guide.html#BASHEK

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#11 Post 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.

Post Reply