I need to read a string from the keyboard in my directdraw-game.. For all other input I've used keyboardstate, but how would I do this?
I use c#, by the way.
I need to read a string from the keyboard in my directdraw-game.. For all other input I've used keyboardstate, but how would I do this?
I use c#, by the way.
Disclaimer: I've never used C# and I'm assuming that there is a message loop, etc, like in a C++ program.
You could use DirectInput. There are some tutorials on that here and here and here.
Or, if you're just trying to read individual keypresses, and not actually read a string, you could handle the WM_KEYPRESS message in your message loop. (Requires knowledge of the WinAPI)
Away.
I was hoping for some other way of doing it, but I guess I'd have to use a C# form to avoid it.
Thanks, though!
Do not call the WinAPI from within DirectX. Those functions are not optimized for games and/or speed. Use DirectInput.
The input in question would only handle the user typing his/her name, so speed isn't really important. I think I got directinput working alright now, tho, so no need for winAPI.
I've had Windows do some nasty stuff if I call the WinAPI too much from within DirectX. They don't get along very well at all, even though MS claims that they should.
You can even see retail games that use the WinAPI to put text on menus. The mouse slows to a crawl and it feels like your dragging it through the mud, to say nothing of actually being able to click on something.
DirectX has everything you need. Stay away from the WinAPI in games unless you are creating/managing threads or accessing the disk - there are more exceptions but...general rule - stay out of the WinAPI while in DirectX.
Create a function that checks input from user (keyboardstate is correct) and log that input into a string.
example:
Create a function in your input class. You'll need a string in your class to store the string as its created. The function will check each legal character to see if it is in the down state. If so, add it to the string. i.e. If 'A' is down, strcat "A" to your string, same for "B", etc, etc. Check for 'shift' to determine upper or lower case. Just keep doing this until the user presses enter (or whatever).
Call this function every frame. Create a boolean flag to like BOOL isUserTypingAString. Set to true when you want, set to false when they press 'enter'. Make your function return without processing anything if !isUserTypingAString.
"There's always another way"
-lightatdawn (lightatdawn.cprogramming.com)
Bubba: Yeah, I've seen examples of this I think. Didn't know that was the reason, tho.
lightatdawn: That's what I did, and also a check to see if the current input is the same as the last (the same keys are still down) to prevent a whole bunch of characters to be input. Works well.
>>also a check to see if the current input is the same as the last
This would prevent the user from typing a strings with the same characters in a row however. An alternate method would be to create a timer. Trip the timer (reset to 0) when a key is pressed. Dont log any further input to your string until the timer has reached a certain delay (maybe 0.2 seconds or something).
"There's always another way"
-lightatdawn (lightatdawn.cprogramming.com)
The check doesn't prevent you from typing the same letter more than once, it just won't let you type more than one letter without releasing the key in between. When the key is released, input will be empty and if you press the key again, the input will differ from the last input..
Heh, I made a whole big thinger for Windows blocking-keyboard-IO for use in my game (i.e. displaying help, hiscores, getting the username, etc.) using Windows API since I didn't feel like learning DirectDraw or anything. Heres the code, if you're interested (and no, it's not the best way of doing it, just the way I came up with first):
**EDIT**
Ahh shoot, it uses a GFX global variable that I made, and in globals.h I have "extern GFX gfx;"... I guess you can still use the code if you convert the code using "gfx" to plain GDI stuff or DirectDraw as the case might be... or you can get the GFX code from the link in my sig (Space Shooterz DX), it might help to understand what's going on under the "gfx.____" calls. It shouldn't be too hard to filter out the GFX stuff, it's only a few lines.
Last edited by Hunter2; 08-02-2003 at 11:37 AM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Thanks, but the one I did works ok I think. Maybe I'll try to improve it later on, though.
Yeah, me too Didn't realize 'til now just how application-specific that code was...
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.