• Welcome to Valhalla Legends Archive.
 

Checking KeyPressed Events

Started by Networks, January 05, 2006, 05:23 PM

Previous topic - Next topic

Networks

I was wondering, if you have a simple console win32 application, how would you go about checking if a key is pressed such as the F1-F12 keys? Also how do you keep checking by running this in the background while the user still is able to provide input into an application? Does it require a timer? What's the best method? Thank you in advance.

warz

#1
Are you checking for input inside of a edit box or on the main dialog? Either way, simply checking for a button to be pressed is similar to the thread I posted about my beeping Enter button. You'll want to subclass the control, if it is one.

You're probably wanting to look for WM_KEYUP, or WM_KEYDOWN.

shout

Quote from: warz on January 05, 2006, 06:05 PM
Are you checking for input inside of a edit box or on the main dialog? Either way, simply checking for a button to be pressed is similar to the thread I posted about my beeping Enter button. You'll want to subclass the control, if it is one.

You're probably wanting to look for WM_KEYUP, or WM_KEYDOWN.

Note that's for a windowed app, he is talking about a console app.

rabbit

The conio library has a getch() function, which does exactly what you want.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Blaze

#4
GetKeyState or GetAsyncKeyState Should work, they're what I use.  I just use a loop with a sleep in it, thats probably bad coding practice though. *shrug* :)
Quote
Mitosis: Haha, Im great arent I!
hismajesty[yL]: No

warz


Skywing

The proper way to do this is by waiting on console input buffer handle (perhaps with WaitForSingleObject), calling GetNumberOfConsoleInputEvents, and then calling ReadConsoleInput appropriately and processing any key events.

Networks

Quote from: Skywing on January 06, 2006, 09:44 AM
The proper way to do this is by waiting on console input buffer handle (perhaps with WaitForSingleObject), calling GetNumberOfConsoleInputEvents, and then calling ReadConsoleInput appropriately and processing any key events.

Would this also work if I was in another window and hit this hotkey or would this only work while the console is waiting for input and I have it focused?

Skywing

Quote from: Networks on January 06, 2006, 10:31 AM
Quote from: Skywing on January 06, 2006, 09:44 AM
The proper way to do this is by waiting on console input buffer handle (perhaps with WaitForSingleObject), calling GetNumberOfConsoleInputEvents, and then calling ReadConsoleInput appropriately and processing any key events.

Would this also work if I was in another window and hit this hotkey or would this only work while the console is waiting for input and I have it focused?

This will obey conventional keyboard input / focus rules.  If you want a global hotkey then you might look into the RegsiterHotKey function.  Remember that only one instance of a program can ever have the same global hotkey registered at the same time.

Networks

Quote from: Skywing on January 06, 2006, 12:57 PM
Quote from: Networks on January 06, 2006, 10:31 AM
Quote from: Skywing on January 06, 2006, 09:44 AM
The proper way to do this is by waiting on console input buffer handle (perhaps with WaitForSingleObject), calling GetNumberOfConsoleInputEvents, and then calling ReadConsoleInput appropriately and processing any key events.

Would this also work if I was in another window and hit this hotkey or would this only work while the console is waiting for input and I have it focused?

This will obey conventional keyboard input / focus rules.  If you want a global hotkey then you might look into the RegsiterHotKey function.  Remember that only one instance of a program can ever have the same global hotkey registered at the same time.

I guess what I am aiming for is a similar function that could possibly use:
GetKeyState or GetAsyncKeyState

Therefore my application would check periodically if a key is being pressed. I am not sure if it's efficient to place something like that under a continous loop.

Skywing

Those two functions operate very differently.  What exactly are you trying to accomplish?  Do you want your hotkey to only work when your program has focus, or always?  What is the expected behavior when the hotkey is already in use by another program? etc..

Mephisto

#11
Quote from: rabbit on January 05, 2006, 10:07 PM
The conio library has a getch() function, which does exactly what you want.

That's incorrect, what he wants is to be able to wait on user input, not prompt them when to enter it.

Quote from: Blaze on January 06, 2006, 02:39 AM
GetKeyState or GetAsyncKeyState Should work, they're what I use.  I just use a loop with a sleep in it, thats probably bad coding practice though. *shrug* :)

It is indeed a bad way to do it.  You should have a wait function implemented to wait on the events specified.  See Skywing's post.

Networks, in addition to what Skywing asked, are you wanting the user to be able to input data (such as chat dialogue, assuming this is a bot you're creating) or other user-needed operations?

Networks

I want the user to press a hotkey at any moment in time regardless of the program's focus and execute a certain function. For example if I am in a game and I press a hotkey I wish for my application to write a certain piece of data to a certain offset.

Mephisto

Quote from: Networks on January 06, 2006, 03:20 PM
I want the user to press a hotkey at any moment in time regardless of the program's focus and execute a certain function. For example if I am in a game and I press a hotkey I wish for my application to write a certain piece of data to a certain offset.

See Skywing's post then.

But in all actuality, it'd be in your best interest, IMO, to have it operate when the program is in focus using standard handle waiting functions (e.g. WaitForSingleObject or MsgWaitForMultipleObjects; whichever of them applies).  There doesn't seem to be any logic in implementing the global hotkey which you want, unless you would like to explain to us what you're trying to have your program accomplish where we may be able to offer more extensive advice.

rabbit

Quote from: Networks on January 06, 2006, 03:20 PM
I want the user to press a hotkey at any moment in time regardless of the program's focus and execute a certain function. For example if I am in a game and I press a hotkey I wish for my application to write a certain piece of data to a certain offset.
Ahh..your intentions are no longer hidden.

Quote from: Mephisto on January 06, 2006, 03:01 PM
Quote from: rabbit on January 05, 2006, 10:07 PM
The conio library has a getch() function, which does exactly what you want.
Eh..that was before I knew he wanted to catch regardless of focus.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.