keyPressed() and getting the special keys

is there any chance to get the “Shift” “ctrl” “alt” and or “cmd” key via the keyPressed() call ?

or any idea for an easy workaround?

Here I pasted the special keys define with preprocessors:

OF_KEY_BACKSPACE, OF_KEY_RETURN, OF_KEY_PRINTSCR, OF_KEY_F1 - OF_KEY_F12, OF_KEY_LEFT, OF_KEY_UP, OF_KEY_RIGHT, OF_KEY_DOWN, OF_KEY_PAGE_UP, OF_KEY_PAGE_DOWN, OF_KEY_HOME, OF_KEY_END, OF_KEY_INSERT

To use:

void keyPressed(int key){

if (key == OF_KEY_LEFT){

// do something
}
}

I hope help you.

best,

Mar

1 Like

I also have the same problem, and why the keyPressed (),ofGetKeyPressed(), response not the shift, ctrl, alt…

# define OF_KEY_CTRL 0x0200
# define OF_KEY_ALT 0x0300
# define OF_KEY_SHIFT 0x0400

*bump*

I’m having the same issue. Can’t figure out what to do with them!?

they might be OS specific key codes?

Command is 55, shift is 56, caps lock 57, option is 58, control is 59.

http://stackoverflow.com/questions/3202629/where-can-i-find-a-list-of-mac-virtual-key-codes

You can check whether they are pressed or not when other keys are pressed using the following:

if (glutGetModifiers() & GLUT_ACTIVE_ALT) doStuff();
if (glutGetModifiers() & GLUT_ACTIVE_CTRL) doStuff();
if (glutGetModifiers() & GLUT_ACTIVE_SHIFT) doStuff();

usually with glut you can just call glutGetModifiers(); which return GLUT_ACTIVE_SHIFT, GLUT_ACTIVE_CTRL, GLUT_ACTIVE_ALT

But I’m trying to figure out what these OF defines are for. pressing ctrl, alt etc. make no difference to the ‘key’ in testApp::keyPressed(), and I can’t find any other ofFunctions which use them.

Much of this will be solved (on the desktop) when we upgrade the windowing system to GLFW, which is scheduled for July 1st :slight_smile: It has a much more robust keyboard input system (e.g. it will actually give you events when modifier keys are pressed!).

the problem with modifiers in glut is that they only work inside the keyPressed/Released event, so there’s no way of knowing reliably if a modifier has been released, that’s why they are not used by OF functions yet. if you want to use them with the current version of OF you’ll need to use glutGetModifiers inside the key events