Created
April 17, 2014 18:00
-
-
Save tgfrerer/11001557 to your computer and use it in GitHub Desktop.
how to poll keys from a windows openFrameworks console window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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