Multiple keypress records per frame?

nobody wrote on Wednesday, December 27, 2006:

hi! i’m using a callback function to catc keyboard events. this works fine, but only ONE keypress is recorded by the callback each frame… this sucks if you want to control player movement, for example - the player has to be able to move forward and strafe left/right at the same time. is there a way to solve this problem?

marcus256 wrote on Thursday, December 28, 2006:

For keeping track of multiple keys at once, you should just use the glfwGetKey() function. It will report the state of any individual key at any given time.

nobody wrote on Thursday, December 28, 2006:

mh. do you plan to expand the callback functionality to support multiple keys? so that not only one key event is sent to the callback function, but an array of events? it’s quite limited like this :confused:

melekor wrote on Thursday, December 28, 2006:

Expanding the callback to support multiple keys makes no sense. Humans don’t move fast enough to physically press two keys at the *exact* same time (with any reliability). No input system that I know of supports this.

I’m pretty sure that what you really want is to detect whether two keys are held down simultaneously. Like Marcus said, glfwGetKey will allow you to do this.

eg:

void PollKeys(){
if(glfwGetKey(GLFW_KEY_UP))
player.y += delta_t;
if(glfwGetKey(GLFW_KEY_DOWN))
player.y -= delta_t;
}

Notice that if you are holding both the up and down keys, player.y will be unchanged.
The PollKeys function call would go in your main loop, and you wouldn’t need the callback anymore. (The callback is useful for other situations.)

nobody wrote on Friday, December 29, 2006:

if i use glfwGetKey the way you suggested it, it works fine for movement etc. but what to do with keystrokes that are to be registered only when they’re pressed and not while they’re held down, e.g. to toggle lgiht sources, menus, etc?
when i use a callback function, then this works fine, but multiple keystrokes are not egistered anymore. it would be useful if glfwGetKey() did not only return true/false but NOT_PRESSED/PRESSED/HELD_DOWN or sth similar… glfwEnable/glfwDisable with GLFW_KEY_REPEAT does only work for the callback function.

thanks :slight_smile:

melekor wrote on Friday, December 29, 2006:

You could use a combination of the callback and glfwGetKey.

example:

void KeyCallback(int key, int action){
// test if CTRL + L was hit
if(action && key == ‘L’ && (glfwGetKey(GLFW_KEY_LCTRL)||glfwGetKey(GLFW_KEY_RCTRL)))
ToggleLightSource();
}