sh: Detect key (CTRL/SHIFT/CAPS LOCK) state?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sh: Detect key (CTRL/SHIFT/CAPS LOCK) state?
# 1  
Old 01-09-2011
sh: Detect key (CTRL/SHIFT/CAPS LOCK) state?

I'm trying to write a shell script that runs periodically and if I'm not at the keyboard, it should do one thing, but if I am at the keyboard, it should do another thing. Therefore I've decided that it would be good if I could press down the CTRL key, (or some other key) and have the shell script detect the key state. I can't use "read" because the script will stop and wait for input, and I can't enter anything if I'm not at the keyboard.
# 2  
Old 01-09-2011
You could try test -t to determine if stdin is a terminal (helps to identify if you running from cron (or piping input):

Code:
 if [ -t 0 ]
then
      echo "Stdin is a terminal
fi

You could add a command line option to the script to prompt from data (or not), eg -b flag for batch mode.

If you really want to read the keystate under and you're using X11 you can use the xset -q command to query a number of things, including the keyboard LED mask. This is pretty hardware/system dependant and will probably not work over ssh connections, with character terminals or windows vt* terminal emulators, etc.
# 3  
Old 01-10-2011
Quote:
Originally Posted by Chubler_XL
You could try test -t to determine if stdin is a terminal (helps to identify if you running from cron (or piping input):

Code:
 if [ -t 0 ]
then
      echo "Stdin is a terminal
fi

You could add a command line option to the script to prompt from data (or not), eg -b flag for batch mode.

If you really want to read the keystate under and you're using X11 you can use the xset -q command to query a number of things, including the keyboard LED mask. This is pretty hardware/system dependant and will probably not work over ssh connections, with character terminals or windows vt* terminal emulators, etc.
None of those solutions will work. It's an initramfs script I'm working on, so there will no X11. I guess there is no simple solution to my problem. Thanks anyway.
# 4  
Old 01-11-2011
So, I made this code, but it's not working and I have no idea why.
Code:
#!/bin/sh

read VAR < /dev/input/event3

if [ -n "${VAR}" ]; then
  echo "Key pressed"
else
  echo "No key pressed"
fi

If I do something like 'cat /dev/input/event3' and press some keys, I get all sorts of garbage on the screen, so I know I can get output from the keyboard, but when I run the script '$VAR' stays empty. If I use a regular non-empty file or some other device, (like the hard disk) the script works.

I'm totally stumped.Smilie Any help would be much appreciated.

Last edited by Ilja; 01-11-2011 at 04:52 PM..
# 5  
Old 01-11-2011
Clearly /dev/input/event3 is not a text file and therefore not suitable for reading with a Shell read. In fact there is little of interest to Shell programmers under /dev. (Yep I know about /dev/null etc.).
Be very very careful in /dev - it is remarkably easy to destroy your system.

What is the output from:
Code:
ls -lad /dev/input/event3
file /dev/input/event3

(Googling the device suggests that it might be a keyboard device on some versions of Linux - a fact of little use to Shell programmers).

What Operating System and version are you running?
What Shell are you running?
What are you trying to do? Can you explain the Project in words just in case there is a stock solution?


There will be kernel driver software between Shell and a keyboard.
If you have "bash" there is a Shell raw "read" with a timeout but even then it will not register keys like ctrl and shift as characters. Reading characters in a tight loop without timeout is CPU-intensive.

A common way to modify the behaviour of a cleverly designed running Shell script is to create a flag file or to use a Shell "trap" to react to a keyboard control code or signal.


What is an "initramfs script", what does it do, and why would you want to interrupt it?

Is it like this:
http://en.wikipedia.org/wiki/Initrd

What runstate are you in while this script is running?

Last edited by methyl; 01-11-2011 at 06:05 PM.. Reason: addenda, grammar, afterthoughts, etc.
# 6  
Old 01-12-2011
Quote:
Originally Posted by methyl
Clearly /dev/input/event3 is not a text file and therefore not suitable for reading with a Shell read. In fact there is little of interest to Shell programmers under /dev. (Yep I know about /dev/null etc.).
Be very very careful in /dev - it is remarkably easy to destroy your system.

What is the output from:
Code:
ls -lad /dev/input/event3
file /dev/input/event3

(Googling the device suggests that it might be a keyboard device on some versions of Linux - a fact of little use to Shell programmers).

What Operating System and version are you running?
What Shell are you running?
What are you trying to do? Can you explain the Project in words just in case there is a stock solution?


There will be kernel driver software between Shell and a keyboard.
If you have "bash" there is a Shell raw "read" with a timeout but even then it will not register keys like ctrl and shift as characters. Reading characters in a tight loop without timeout is CPU-intensive.

A common way to modify the behaviour of a cleverly designed running Shell script is to create a flag file or to use a Shell "trap" to react to a keyboard control code or signal.


What is an "initramfs script", what does it do, and why would you want to interrupt it?

Is it like this:
initrd - Wikipedia, the free encyclopedia

What runstate are you in while this script is running?
Now, here's my problem. I'm trying to unlock my encrypted system disk in Ubuntu 10.10. Sometimes I want to enter the passphrase via SSH and sometimes locally. Here's the piece of code that needs to be modified.
Code:
if [ -x /bin/plymouth ] && plymouth --ping; then
  cryptkeyscript="plymouth ask-for-password --prompt"
  cryptkey=$(echo -e "$cryptkey")
else
  cryptkeyscript="/lib/cryptsetup/askpass"
fi

I need the script to run the first case if I want to enter the passphrase via the local keyboard, and run the second case if I want to enter the passphrase via SSH.
Code:
if [ -x /bin/plymouth ] && plymouth --ping && #I'm at the keyboard#; then
  cryptkeyscript="plymouth ask-for-password --prompt"
  cryptkey=$(echo -e "$cryptkey")
else
  cryptkeyscript="/lib/cryptsetup/askpass"
fi

So, how do I notify the computer that I am at the keyboard? By pressing a key, perhaps.
# 7  
Old 01-12-2011
Does "at the keyboard" mean "on the console" ?

This post is very difficult to understand for some reason. Maybe I'm being thick.

Quote:
I'm trying to write a shell script that runs periodically
This implies a regular cron job or some other automated job (hence the reply in post #2). This is clearly? not the case.

Best I can suggest is running the unix command "tty" when "at the keyboard". Hopefully it will reply "/dev/console". If it does, we have a simple way of distingishing where we are.


Btw. It would really help if you answered the questions.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate all possible word with caps and no caps

With use of sed/awk, how can I print all possible combinations of a word with caps/non-caps. Eg Applying operation on "cap" should generate output as follows. cap CAP Cap cAp caP CAp cAP CaP (12 Replies)
Discussion started by: anil510
12 Replies

2. Shell Programming and Scripting

How to detect key press in cgi shell script?

I want to detect key pressed in my .cgi web page, but it does not work even I found the code in other web site. My code is : #!/bin/sh #================================================= # PATH defination # ================================================... (2 Replies)
Discussion started by: Shuinvy
2 Replies

3. UNIX for Dummies Questions & Answers

Caps lock problem

hi all this s quite a foolish problem. I seem to hav pressed some keys s.t in unix, my letters are comin in caps and with caps lock on, i am getting lowercase letters. :o Pls help. Also is there any reference or manual where i can check in case such problems arrise? thanx in advance curiosity (4 Replies)
Discussion started by: curiosity
4 Replies

4. UNIX for Advanced & Expert Users

Set Caps and Num lock from within X?

Hello, Not sure if this is the right place to post it but.. I have a requirement to set Caps lock and/or Num lock on and off via a Cron job. Now I have working scripts that do the job, but as soon as X starts up the jobs no longer run (well they appear to, but Caps lock and Num lock do not... (0 Replies)
Discussion started by: autotuner
0 Replies

5. Shell Programming and Scripting

How to issue ctrl+D and enter key

My problem is that i have to connect Linux server. I can connect it with SSH but because of the software of the Linux server, i need to press enter and after ctrl+D. Therefore, I need to write a script that should press enter and ctrl+D. How can i write it in KSH shell script. HELP ME! (7 Replies)
Discussion started by: fozay
7 Replies

6. HP-UX

Caps lock dtterm

Hello, We are having a problem with running dtterm off a RHEL server. Logging into an HP-UX server from a RHEL 5.1 desktop, setting DISPLAY and running dtterm, the caps lock does not work. We have been playing with xmodmap & stty but to no avail. Any help appreciated. mgb (7 Replies)
Discussion started by: mgb
7 Replies

7. Red Hat

Caps lock dtterm

Hello, We are having a problem with running dtterm off a RHEL server. Logging into an HP-UX server from a RHEL 5.1 desktop, setting DISPLAY and running dtterm, the caps lock does not work. We have been playing with xmodmap & stty but to no avail. Any help appreciated. mgb (1 Reply)
Discussion started by: mgb
1 Replies

8. Shell Programming and Scripting

How to delete ctrl key values in a given string?

Hi all, My query is... in the runtime, you are getting any input string. Unfortunately, you have pressed some ctrl keys or esc keys or arrow keys while typing input string. You can get the input value like that... input string as welcome^ So ,I want to remove those unwanted keys... (4 Replies)
Discussion started by: balan_mca
4 Replies

9. Shell Programming and Scripting

Disabling ctrl-Z key inside shell script

Hi I have tried to disable the CTRL-Z key inside a shell(sh) script using the command trap "`echo "Ctrl-Z key disabled"`" 20But I am not able to exit from the script after pressing CTRL-Z key. How to proceed this? Need reply soon (11 Replies)
Discussion started by: suganthic
11 Replies
Login or Register to Ask a Question