|
Page 1 of 1
|
[ 9 posts ] |
|
Direct Input - detecting 'any' key press
| Author |
Message |
|
entell
Joined: Fri Feb 21, 2003 8:01 am Posts: 2
|
 Direct Input - detecting 'any' key press
I have a question regarding detecting what key is pressed. I am wondering if anyone knows of a good way to detect 'any' key press. It is easy enough to detect if a particular key is pressed, but what if you don't really care what it is, or what if you want to detect a range of keys, like A through Z?
I can think of one way which is to do the following for each key to be detected:
#define KEY_DOWN(key) (key_buffer[key] & 0x80) If(KEY_DOWN(DIK_A)) // Do something
...
If(KEY_DOWN(DIK_Z)) // Do something
Or put the KEY_DOWN in a for loop or something crude like that. Anyone have a better approach?
The application would be to get a string from the user such as the player's name... What I am really trying to simulate is getch() or scanf() where I don't really have to worry what key is pressed.
I searched around but I couldn't find a similar post, so if this question has been asked before, I am sorry for repeating it.
Thanks for your help!
|
| Thu Mar 27, 2003 10:20 pm |
|
 |
|
Omen
Joined: Wed Sep 29, 1999 7:01 am Posts: 59 Location: North Shore City, Auckland, New Zealand
|
 Direct Input - detecting 'any' key press
memcmp() can tell you if at least one key down, but you'd still have to loop thru to find out which key exactly
you could set up an empty array like:
char no_keys_down[256]; memset(no_keys_down, 0, sizeof(char)*256);
then to see if a key is down:
if(memcmp(no_keys_down, keys, 256) != 0) { for(int i = 0; i < 256; i++) { if(KEY_DOWN(i)) { // .... } } }
|
| Fri Mar 28, 2003 3:25 am |
|
 |
|
brad321
Joined: Thu Dec 02, 1999 8:01 am Posts: 1
|
 Direct Input - detecting 'any' key press
You could also check for WM_KEYDOWN
|
| Sat Mar 29, 2003 7:55 am |
|
 |
|
entell
Joined: Fri Feb 21, 2003 8:01 am Posts: 2
|
 Direct Input - detecting 'any' key press
I have been thinking about this, but can't find anything simpler than checking the status of every single key on the keyboard. Checking for WM_KEYDOWN is the same thing. Once I know there is a key press, how do I know which key it was?
Think of a scenario where you are asking for the player's name for the game or for "hall of fame". I guess at that point since the computer is not really doing anything time critical other than reading the keyboard and printing on the screen, so it can waste all the time it has to scan for keys.
How about those RTS games with all kinds of keys defined to do stuff? Like Civilization... Do these games scan for every blessed key to see if it was pressed? I guess so.. No wonder such complex games have millions of lines of source code. [img]images/smiles/icon_smile.gif[/img]
I think I get the idea. Since everything is event driven, there is no equivalent of "getch()" in the Windows environment, but it is so ugly to have to have so many if's (or a huge switch statement) just to check if the user pressed anything.
Oh well... That's life!
|
| Tue Apr 08, 2003 7:50 pm |
|
 |
|
Yoshi
Joined: Sun Nov 04, 2001 8:01 am Posts: 10 Location: NYC
|
 Direct Input - detecting 'any' key press
<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">quote:</font><hr>Originally posted by entell: <strong>I have been thinking about this, but can't find anything simpler than checking the status of every single key on the keyboard. Checking for WM_KEYDOWN is the same thing. Once I know there is a key press, how do I know which key it was?
Think of a scenario where you are asking for the player's name for the game or for "hall of fame". I guess at that point since the computer is not really doing anything time critical other than reading the keyboard and printing on the screen, so it can waste all the time it has to scan for keys.
How about those RTS games with all kinds of keys defined to do stuff? Like Civilization... Do these games scan for every blessed key to see if it was pressed? I guess so.. No wonder such complex games have millions of lines of source code. [img]images/smiles/icon_smile.gif[/img]
I think I get the idea. Since everything is event driven, there is no equivalent of "getch()" in the Windows environment, but it is so ugly to have to have so many if's (or a huge switch statement) just to check if the user pressed anything.
Oh well... That's life!</strong><hr></blockquote>
You could use a look-up table of some type...
|
| Tue Apr 08, 2003 8:50 pm |
|
 |
|
jd
Joined: Thu Apr 26, 2001 7:01 am Posts: 19 Location: Midland
|
 Direct Input - detecting 'any' key press
if you just want to know if any key was pressed , do this:
<blockquote><font size="1" face="Verdana, Helvetica, sans-serif">code:</font><hr><pre>WinProc() { //foo case WM_KEYDOWN: { //a key was pressed } }</pre><hr></blockquote>
|
| Tue Apr 08, 2003 9:19 pm |
|
 |
|
Omen
Joined: Wed Sep 29, 1999 7:01 am Posts: 59 Location: North Shore City, Auckland, New Zealand
|
 Direct Input - detecting 'any' key press
if you want to use the old slow WindowProc() way then yes WM_KEYDOWN can tell you the key:
case WM_KEYDOWN: g_LastPressedKey = wParam; break;
where wParam is VK_ENTER, VK_F1, VK_ESCAPE etc etc you can find these constants in the windows header files.
the two games in my sig both ask the user for their name. i have a lookup table of characters to keypresses, not unlike the following:
char* KeyCharacters[MAX_KEYS];
and use the DirectInput contants to index into this table, e.g.:
if(PressedKeys[DIK_UP] & 0x80) { printf("you pressed %c\n", KeyCharacters[DIK_UP]); }
you can setup the table like so:
void SetupKeyTable() { KeyCharacters[DIK_F1] = "F1"; KeyCharacters[DIK_F2] = "F2"; KeyCharacters[DIK_F3] = "F3"; KeyCharacters[DIK_F4] = "F4"; KeyCharacters[DIK_F5] = "F5"; KeyCharacters[DIK_F6] = "F6"; ...etc KeyCharacters[DIK_A] = "A"; KeyCharacters[DIK_B] = "B"; KeyCharacters[DIK_C] = "C"; KeyCharacters[DIK_D] = "D"; ...etc }
in my games i loop thru the all keys to detect any key press. we're talking about 104 keys on the keyboard so that is not much processing at all, even for older computers. remember, computers to millions of instructions per sec, 104 if tests is would be done in microsecond time.
char* GetCurrentlyPressedKey() { for(int i = 0; i < MAX_KEYS; i++) { if(PressedKeys[i] & 0x80) return KeyCharacters[i]; } return NULL; }
to use it:
int WINAPI WinMain(...) { char name[64]; for(; [img]images/smiles/icon_wink.gif[/img] { // dispatch win msgs char* key = GetCurrentlyPressedKey(); if(key) { strcat(name, key); } } // flip buffers }
i'm doing this sorta thing in my 2 games and it works fine. obviously you'll have to handle shift keys explicitly to swap from upper to lower case and vice versa, ie. translate 'a' to 'A' etc
|
| Wed Apr 09, 2003 8:59 pm |
|
 |
|
Yoshi
Joined: Sun Nov 04, 2001 8:01 am Posts: 10 Location: NYC
|
 Direct Input - detecting 'any' key press
Depending on how directX sets up it's key array you might be able to figure out the ASCII offset at least for certain ranges. Then if it's in a range where you know there's a linear relation with the index and the ASCII value. For example if DIK_A was 75, DIK_B 76, etc. then you could do something like:
char cPressed = static_cast<char>(index - 10);
Although I don't know if this is exactly what you are looking for.
|
| Wed Apr 09, 2003 10:21 pm |
|
 |
|
Aprosenf
Joined: Sat Apr 21, 2001 7:01 am Posts: 53 Location: Lexington, MA, USA
|
 Direct Input - detecting 'any' key press
Actually, the numerical values of DIK_A through DIK_Z follow the layout on the keyboard. DIK_TAB, DIK_Q, DIK_W, ..., DIK_P, DIK_LBRACKET, DIK_RBRACKET, DIK_BACKSLASH, ... DIK_A, DIK_S, DIK_D, ..., DIK_Z, DIK_X, ..., DIK_M, DIK_COMMA, DIK_PERIOD, DIK_SLASH form an arithmetic sequence.
|
| Wed Apr 09, 2003 10:35 pm |
|
 |
|
|
Page 1 of 1
|
[ 9 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 2 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|