Reputation: 620
I would like to start a julia script by using the keyboard shortcut Ctrl+Shift+H
.
When the script starts, the keys Ctrl
and Shift
may still be pressed (depending on how quickly I remove my fingers).
I would like to be sure that these two keys are released before executing the rest of the script. To do that I need to test if the key are pressed.
Note that triggering an event when the key are released would not be enough as the keys may not be pressed when the script starts.
I found several references to detect when a key is pressed in Julia but I did not find one to test if a key is currently pressed.
Do you know how I could do that?
Here is a minimal example of why I would like to do that. You can find here a file in the cnee format which enables to write "azerty" in the window which has the focus. If you type the following command in a terminal, "azerty" is typed in it as expected:
cnee --replay -sp 0 -f ./recorded_macro.xnl
In order to write "azerty" in any window (not just terminals), I create a keyboard shortcut Ctrl+Shift+H
which executes this command. If I use the shortcut, the keys Ctrl
and Shift
are likely to be pressed when the command is executed (except if I use the shortcut very quickly) and instead of typing "azerty", the window with the focus will get Ctrl+Shift+a
, Ctrl+Shift+z
, Ctrl+Shift+e
, Ctrl+Shift+r
, Ctrl+Shift+t
and Ctrl+Shift+y
which will trigger actions in the window but will not write "azerty".
So, I would like instead to start a python script with this shortcut which waits for Ctrl
and Shift
to be non-pressed before executing the cnee
command. Note again that waiting for the keys to be released with a listener is not a solution as the keys Ctrl
and Shift
may not be pressed at the start of the python script if I use the shortcut quickly (and so I will have to press again Ctrl
and Shift
before executing the cnee command which I do not want).
Upvotes: 3
Views: 444
Reputation: 6086
The following Gtk program will detect the release of shift, control, and alt that are already pressed before the program starts or gets the focus. Note that my keyboard at least seems to not always detect multiple keystrokes, so perhaps depending on your keyboard you may have to just detect one of those releases.
using Gtk
function keypresswindow()
txt = "Press and Release a Key"
state = ""
win = GtkWindow("Key Release Test", 500, 30) |> (GtkFrame() |> ((vbox = GtkBox(:v)) |> (lab = GtkLabel(txt))))
function keyreleasecall(w, event)
event.keyval == 65505 && (state *= "SHIFT ")
event.keyval == 65507 && (state *= "CONTROL ")
event.keyval == 65513 && (state *= "ALT ")
set_gtk_property!(lab, :label, "You have released: $state")
end
signal_connect(keyreleasecall, win, "key-release-event")
cond = Condition()
endit(w) = notify(cond)
signal_connect(endit, win, :destroy)
showall(win)
wait(cond)
end
keypresswindow()
Upvotes: 1