multiple keys and key related questions


i do have several questions about keys:

  1. is there any way to track multiple keys pressed ?

i don’t mean the special keys plus another key, but something like the key “a” plus key “b” being pressed at the same time ?

  • for the special keys:

i can figure out which special keys are pressed by the numbers that i get for the key:

just a:
key: 97

shift + a:
key: 65
SHIFT pressed.

ctrl + a:
key: 1
CONTROL pressed.

key: 1
shift + ctrl + a

alt + a:
key: 97
ALT pressed.

alt + shift + a
key: 65

alt + ctrl + a
key: 1

—> is there a way to _always_ get the glutGetModifiers() ?
(in case of shift + ctrl + a for instance the only way for me to see which special keys are pressed is the actual number of the key)

i am on a german keyboard, and for the umlaut keys and the german ß (ss) i get two numbers:
key: 195
key: 188

key: 195
key: 182

key: 195
key: 164

key: 195
key: 159

—> is there a way to only get only one number ?

(for the key - on the german keyboard - on the left of the DELETE key i get no number at all)

  1. is there a possibility to check for keys outside of the void keyPressed function ?

if i try to check for keys inside the update function for instance then i get this error:
“glutCurrentModifiers: do not call outside core input callback.”

i think this is the same question like here:,-shift,-alt-keys/927/7

  1. is there any place where i could change the time after which the key press is recognized as a repeated key when i keep holding the key down ?
    (if i press and hold down a key, only after a while it is recognized as still being pressed)

any hints in any directions would be great!
(sorry if there are too many questions at the same time, but i thought they all belong to the same thread…)

all the best


Hey Lia,

Questions 1, 4 and 5 can be addressed using the same solution. An array of booleans:

boolean keyIsDown[255];  
void keyPressed(int key) {  
   keyIsDown[key] = true;  
void keyReleased(int key) {  
   keyIsDown[key] = false;  
void update() {  
   if(keyIsDown['a'] && [keyIsDown['b']) {  
      // do whatever you wanna do if both a and b are down.  

note this will track for every key and differentiate between uppercase and lowercase. You can do additional logic to condense those.

1 Like

hey memo,

thanks a lot for this !
it already helps a lot !!!

for questions 2) and 3):

in principle i would like to test all sorts of key combinations.

as a basic problem:
if i want to check for the Shift key and (let’s say) the key “a” being pressed at the same time, no matter which key i press first, then this doesn’t really work.

if i press Shift first and hold it and after that i press “a”, then i get the uppercase A and can check for it.
But if i first press “a”, hold it, and then press “Shift”, it doesn’t capture the Shift being hold down (and it also doesn’t change the “a” to an uppercase A)…

after all i would like to be able to track for instance Shift, Ctrl, Option plus one letter key, but it should not matter, which key is pressed or released at what time… (it should just check for all the keys currently being hold down)

all the best

Lia.- // (hoping not to be too weird with my explanations!)

Hmm… I am wondering if there’s a way to just detect when the Shift key is pressed/released on its own?

ok… so I found this-post about detecting the shift key… but glutGetModifiers() only works from inside the key event handlers… which only get triggered if there’s another keystroke along with the Shift key.

I created an addon for keeping track of pressed keys. It also handles shift, control and alt.…

in testApp.h:

#include "ofxKeyMap.h"  
ofxKeyMap keys;  

for example in testApp.cpp:

if (keys.isKeyDown('a') && keys.isKeyDown(OF_KEY_LEFT)) ....  

Hello guys

A little minor change is needed to get the ofxKeyMap working.
in the header file, add the folowing line

#include "assert.h"  

(needed on code::blocks 10.05 win vista)

thanks rick!

I am looking for handling the SHIFT or CTRL key down on his own (with no other key), and hoped your solution works, but :
for Windows you use GetKeyState so that’s OK
for other platform you still use glutGetModifiers ???

Did you try it on Mac or Linux ? Is the glutGetModifiers different on those platform ?