Skip to content

Instantly share code, notes, and snippets.

@tgfrerer
Created April 17, 2014 18:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgfrerer/11001557 to your computer and use it in GitHub Desktop.
Save tgfrerer/11001557 to your computer and use it in GitHub Desktop.
how to poll keys from a windows openFrameworks console window
void ofApp::pollKeys(){
HANDLE hStdInput = GetStdHandle(STD_INPUT_HANDLE);
DWORD events = 0; // how many events took place
INPUT_RECORD input_record; // a record of input events
DWORD input_size = 1; // how many characters to read
// we use peek so that this is non-blocking.
BOOL peek = PeekConsoleInput(hStdInput, &input_record, input_size, &events);
if(peek && input_record.EventType == KEY_EVENT)
{ // PeekConsoleInput succeeded and a key was pressed, so set and return keypress.
(input_record.Event.KeyEvent.bKeyDown) ?
ofNotifyKeyReleased(input_record.Event.KeyEvent.wVirtualKeyCode) :
ofNotifyKeyPressed(input_record.Event.KeyEvent.wVirtualKeyCode);
// remove any other input events from stdin.
FlushConsoleInputBuffer(hStdInput);
} else {
// remove any non-key_events from input buffer.
FlushConsoleInputBuffer(hStdInput);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment