Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a way to detect when a key is released, similar to

EventHandler[InputField[], {"KeyDown", "k"} :> Print["k pressed"]]

It seems such detection isn't available at first glance, perhaps it can be achieved some other way?

Alternatively is there a way to detect which keys are currently pressed? I know that;

CurrentValue["ModifierKeys"] 

Works for shift ctrl and such, but I can't seem to figure out how to get a list of regular keys being pressed.

Edit: CurrentValue["EventKey"] does not seem to work for this purpose as It only detects keydown events, thus you cannot do something like the following which detects shift-up

shiftDown = False;
Dynamic[
If[MemberQ[CurrentValue["ModifierKeys"], "Shift"], (shiftDown = True;), 
      If[shiftDown, (shiftDown = False; Print["Shift up code"])]
    ]
]

Mathemtaica.comp.sys- comp.soft-sys.math.mathematica post

share|improve this question
2  
paging Yu-Sung Chang... – rm -rf May 6 '12 at 19:12
3  
The fact that the KeyDown event fires continuously while the key is pressed seems coherent with a timed scan of the keyboard, and not a real (interrupt driven) event handler. If that is the case, you'll never get a KeyUp event. – belisarius May 6 '12 at 19:21
If anyone knows for sure if this is the case, it would be nice with confirmation of this. – jVincent May 6 '12 at 20:01
Thanks for the edit. The reason is to keep things together for readers of both threads. – Szabolcs May 7 '12 at 21:20

2 Answers

To answer the second part of your question, you can use CurrentValue["EventKey"] to get the current key that is being pressed. Modifying your example above:

EventHandler[InputField[], "KeyDown" :> Print[CurrentValue["EventKey"]]]
share|improve this answer
Thank you for your answer. I realize that you can detect which key triggered the event inside an event handler, however for the second part of my question I was looking for a way to cercumvent the lack of a keyup event, by having something similar to Dynamic[CurrentValue["ModifierKeys"]] which can be used to along with tricks to for example detect when shift is lifted. – jVincent May 6 '12 at 19:25

You can try using NotebookEventActions see the given example in an other subject.

displaying = True;

SetOptions[EvaluationNotebook[], 
 NotebookEventActions :> {{"KeyDown", "k"} :> (If[! displaying, 
      Print["k press"]]), 
   "DownArrowKeyDown" :> (If[! displaying, Print["down press"]]), 
   "MouseClicked" :> (If[displaying, displaying = False])}]

the related thread Make EventHandler work for clicks and keys in a Dynamic display

share|improve this answer
1  
I fear this has the exact same problem, you cannot detect keyUp, since events only trigger on keyDown. – jVincent May 6 '12 at 19:50
3  
If it is really so crucial you may use JLink to call java API in mathematica. There defined key up events. – s.s.o May 6 '12 at 20:04

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.