I want to use Autokey to toggle the e key on and off in a program called "xyz". So if I press e, the program thinks I'm holding down e. I press e again, it thinks I released it. How do you do this please?

Or is there some other program that can do this?

bonus question: how do you do this with any Ubuntu program, such as fvwm2, xorg, xmodmap, xkb, xrdb, any way at all?


I'm not familiar with autokey, but this looks doable. Use Keyboard.press_key to send a key press event and Keyboard.release_key to send a key release event. To remember which one to send, you can use Store.set_value and Store.get_value.

Alternatively, you can use xdotool to inject input events into a window and xprop to attach data to a window. Run the following shell command (untested) to send alternate press/release events to the active window.

window_id=$(xdotool getactivewindow)
case $(xprop -id "$window_id" -f key_e_is_down 32c -notype key_e_is_down) in
  *1) key_command=keydown; new_state=0;;
  *) key_command=keyup; new_state=1;;
xdotool "$key_command" "e"
xprop -id "$window_id" -f key_e_is_down 32c -set key_e_is_down "$new_state"

If you want to bind this action to a key, but to have the key active only in one window, I'm not sure if Autokey can help you. The easiest way to do that is from the window manager, but most window managers can't do it. I use sawfish as my window manager, which can do this without any external tool.

(define (toggle-e w)
  (interactive "%W")
  (let ((down (window-get w 'key-e-is-down)))
    (synthesize-event (if down "Release+e" "e") w)
    (window-put w 'key-e-is-down (not down))))
(define xyz-window-map (make-keymap))
(bind-keys xyz-window-map "e" toggle-e)
(add-window-matcher 'WM_CLASS "^Xyz/" `((keymap . ,xyz-window-map)))
  • Thanks for an interesting answer Gilles. The case line in the shell script i changed to: 'xprop -id "$window_id" -f key_e_is_down 32c -notype|grep key_e_is_down|cut -f3' which seems a bit excessive, but xprop doesn't seem to have a get property function. xdotool is interesting but it seems to only send characters until some other keyboard event comes along, which isn't enough. It would have to keep sending until toggled. Tried doing it using fvwm2 but that didn't work out either. Thanks again. – user111973 Apr 27 '15 at 5:27
  • @user111973 To get a property using xprop, add the name of the property at the end. I somehow removed that when I was editing the script, I've added it back. The xdotool and sawfish snippets I wrote only send a key press event, and later a key release event; they don't keep sending key press events to simulate keyboard repeat, because that wasn't a requirement in your question. Doing that would be possible, but more work, I think you'd need to keep a program running to do just that. Do you need the key to be sent even while xyz isn't the active window? – Gilles Apr 27 '15 at 9:31
  • I thought sending a keydown without a keyrelease would make the program think i was holding it down, but apparently not. Also the program might be rejecting synthetic events, so i might be forced into a hardware solution. Thanks again. – user111973 Apr 27 '15 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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