Reputation: 1163
I was using Windows.h library's GetAsyncKeyState function to define which key was pushed on the keyboard . Here is the program which returns the int value of the pushed key for example.
#include <iostream>
#include <windows.h>
using namespace std;
int main (){
char i;
for(i=8;i<190;i++){
if(GetAsyncKeyState(i)== -32767){
cout<<int (i)<<endl;
}
}
return 0;
}
But now I am trying to make an openGl very simple game and this function appeared to be very slow for that . Is there a function which will not need a for cycle 180 times and an if statement .
Thanks
Upvotes: 0
Views: 4946
Reputation: 1915
You can use a combination of PeekMessage
and TranslateMessage
and DispatchMessage
. It's quite simple to register your own windows procedure (WNDPROC) to process input messages from windows.
MSG msg;
while(PeekMessage( &msg, NULL, 0, 0 ))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
The DispatchMessage
function calls your registered procedure. See: this for more info.
Upvotes: 1
Reputation: 2310
You're trying to make a opengl game, so you're probably going to use GLFW. If that's the case then you can get away with using glfwGetKey()
as such:
if (glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS) {...}
if (glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS) {...}
That method is nicely explained in tutorial 6 from opengl-tutorial.org. But you should probably start from the first tutorial of beginners tutorials and later intermediate tutorials.
If you're going to make the win32 window by yourself you can listen for the WM_KEYDOWN
or WM_KEYUP
messages and get the virtual key code from wparam. After a minute of googling I found you these two sites for examples here and here.
Upvotes: 1