Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to detect if a key is being pressed when running a script. I have the following script:

#!/bin/bash
sleep 0.5
xte 'str sometext'

I run this script using a shortcut to paste "sometext" where I'm at in kde. I used a sleep 0.5 here because the script itself is run with a shortcut that uses ctrl and alt. If the sleep 0.5 is omitted, the result is that ctrl+s, ctrl+o, etc... is sent. I think the solution would be to add some bussy wait at the beginning that would block untill no keys are being pressed. How can I get this to work in the script?

share|improve this question
    
Not sure I'm getting the right picture here (is that the whole content of your script ?) so posting as a comment. You could ditch sleep and xte and use xdotool instead, e.g.: xdotool type --clearmodifiers "sometext". Works OK if I run the script with shortcuts like Ctrl+Alt+K assigned via Gnome settings. –  don_crissti Jun 12 '13 at 12:32

1 Answer 1

up vote 2 down vote accepted

Since you are using xte anyways, why not release the keys with xte? Something along the lines

xte "keyup Control_L"
xte "keyup l"

(assuming your shortcut is ctrl-l).

share|improve this answer
    
ok this works for the alt key and control key, the only problem is that I have my capslock be another control key so the keyup on the control leaves the capslock down. I have searched for a way to keyup capslock but this seems not to be possible with xte. any suggestions? –  Silverrocker Oct 15 '12 at 21:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.