Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 01 Sep 2014, 22:26
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to tell which key was pressed?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [11 Posts]  
Author Message
R-S-H

Joined: 18 Feb 2013
Posts: 490

PostPosted: 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
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 3964
Location: Kiel,Germany

PostPosted: 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
View user's profile Send private message Visit poster's website 
Flash
Official Dog Handler


Joined: 04 May 2005
Posts: 11034
Location: Arizona USA

PostPosted: Wed 08 May 2013, 13:01    Post subject:  

Yes, xev has worked for me. Smile

R-S-H, you might find the answer toward the end of this thread. Here, maybe.
Back to top
View user's profile Send private message 
R-S-H

Joined: 18 Feb 2013
Posts: 490

PostPosted: 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! Laughing

_________________
LazY Puppy Home
The new LazY Puppy Information Centre

Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2505
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: 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
Code:
xev
Question
Back to top
View user's profile Send private message 
R-S-H

Joined: 18 Feb 2013
Posts: 490

PostPosted: 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
View user's profile Send private message 
Keef


Joined: 20 Dec 2007
Posts: 622
Location: Staffordshire

PostPosted: 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
View user's profile Send private message 
SFR


Joined: 26 Oct 2011
Posts: 1063

PostPosted: 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. 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:
#!/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
View user's profile Send private message 
R-S-H

Joined: 18 Feb 2013
Posts: 490

PostPosted: Wed 08 May 2013, 16:40    Post subject:  

Hi.

SFR wrote:
it detects keystrokes globally

That's exactly how it would used to be! Very Happy

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! Very Happy

RSH

_________________
LazY Puppy Home
The new LazY Puppy Information Centre

Back to top
View user's profile Send private message 
muggins

Joined: 20 Jan 2006
Posts: 6683
Location: lisbon

PostPosted: 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
View user's profile Send private message 
Flash
Official Dog Handler


Joined: 04 May 2005
Posts: 11034
Location: Arizona USA

PostPosted: 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
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [11 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0684s ][ Queries: 11 (0.0037s) ][ GZIP on ]