Keyboard repeat


I notice that in Component::keyPressed(…) I get no info on keyboard repeat ? I need to implement my own repeat rate, and thus be able to know when a
KeyPress object is in repeat or not.



You can get key up/down events, so could use those to do your own auto-repeat.


Ok, bringing this old thread back to life. I now would need to know when a key is pressed, but I’m only interested in the first event, i.e. I want to ignore subsequent events due to auto-repeat. KeyListener::keyStateChanged seems a bit too “coarse”, it would probably do the job, but not easily. Is it not possible to add a repeat flag to the KeyPress class ? I’m pretty sure that Windows supports that info, not 100% about Mac/Linux though…


It’d take quite a bit of messing about and researching all the different platforms… Not something I want to embark on just now.


for something similar i use a sorted set, works perfect

detect keypress

bool MainEditor::keyPressed (const KeyPress& key, Component* originatingComponent)
if (!pressedKeys.contains(key.getKeyCode()))


detect keyUp

bool keyStateChanged (bool isKeyDown,Component* originatingComponent) { if (!isKeyDown) { for (int i=oressedKeys.size()-1; i>=0; i--) { int keyCode=pressedKeys[i]; if (!KeyPress::isKeyCurrentlyDown(keyCode)) pressedKeys.remove(i);

And then use a timer for you own auto-repeat


Thanks chkn, I’ll do something similar.

Jules, a question though: For keyStateChanged, does events get coalesced or if I press (or release) 4 keys at the same time, will I get 4 events ?


I think that’ll depend on the OS… Could go either way, so you should make your code deal with both cases.


you will get 4 keypresses, but not always the right keystate, i think that has to do with hardware restrictions or how the keyboard-controller works…
modifier keys like strg+alt+shift work differently than other keys


It appears there is still no native support for handling physical keypresses (i.e. ignoring auto-repeat-s).

I'm wishing to generate an event whenever a key is activated or released. The problem is on some skittish machines a heavy press generates unwanted repeats.

+1 on robiwan's suggestion for an KeyPress::isRepeat flag

I've dug into direct keyboard mapping on Windows and OSX in the past and it is awkward, processing keyboard events is a horrible tangle on any OS (especially the TAB key, good luck ever getting any sensible response out of that key).

But isn't the ability to respond to keyDown and keyUp events a very fundamental core requirement?  Especially for a framework focused on audio apps -- noteOn noteOff etc?

Couldn't JUCE maintain an internal set of currently down keys, as in chkn's answer, and use this to set a KeyPress::isRepeat flag?