Author | Message | Time |
---|---|---|
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. | January 5, 2006, 11:23 PM |
warz | 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. | January 6, 2006, 12:05 AM |
shout | [quote author=warz link=topic=13803.msg140712#msg140712 date=1136505938] 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. [/quote] Note that's for a windowed app, he is talking about a console app. | January 6, 2006, 3:45 AM |
rabbit | January 6, 2006, 4:07 AM | |
KkBlazekK | 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* :) | January 6, 2006, 8:39 AM |
warz | Oh, console app. Sorry. | January 6, 2006, 12:47 PM |
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. | January 6, 2006, 3:44 PM |
Networks | [quote author=Skywing link=topic=13803.msg140813#msg140813 date=1136562248] 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. [/quote] 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? | January 6, 2006, 4:31 PM |
Skywing | [quote author=Networks link=topic=13803.msg140817#msg140817 date=1136565103] [quote author=Skywing link=topic=13803.msg140813#msg140813 date=1136562248] 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. [/quote] 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? [/quote] 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. | January 6, 2006, 6:57 PM |
Networks | [quote author=Skywing link=topic=13803.msg140831#msg140831 date=1136573855] [quote author=Networks link=topic=13803.msg140817#msg140817 date=1136565103] [quote author=Skywing link=topic=13803.msg140813#msg140813 date=1136562248] 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. [/quote] 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? [/quote] 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. [/quote] 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. | January 6, 2006, 8:19 PM |
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.. | January 6, 2006, 8:32 PM |
Mephisto | [quote author=rabbit link=topic=13803.msg140754#msg140754 date=1136520469] The conio library has a getch() function, which does exactly what you want. [/quote] That's incorrect, what he wants is to be able to wait on user input, not prompt them when to enter it. [quote author=Blaze link=topic=13803.msg140788#msg140788 date=1136536746] 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] 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? | January 6, 2006, 9:01 PM |
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. | January 6, 2006, 9:20 PM |
Mephisto | [quote author=Networks link=topic=13803.msg140843#msg140843 date=1136582407] 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. [/quote] 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. | January 6, 2006, 10:37 PM |
rabbit | [quote author=Networks link=topic=13803.msg140843#msg140843 date=1136582407] 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. [/quote]Ahh..your intentions are no longer hidden. [quote author=Mephisto link=topic=13803.msg140839#msg140839 date=1136581289] [quote author=rabbit link=topic=13803.msg140754#msg140754 date=1136520469] The conio library has a getch() function, which does exactly what you want. [/quote] [/quote]Eh..that was before I knew he wanted to catch regardless of focus. | January 7, 2006, 3:55 AM |
KkBlazekK | [quote author=rabbit link=topic=13803.msg140895#msg140895 date=1136606133] [quote author=Networks link=topic=13803.msg140843#msg140843 date=1136582407] 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. [/quote] Ahh..your intentions are no longer hidden. [/quote] It was pretty obvious what he was doing. Networks, use what Skywing said, RegisterHotKey it should work well. | January 7, 2006, 9:40 AM |
Mephisto | [quote author=rabbit link=topic=13803.msg140895#msg140895 date=1136606133] [quote author=Networks link=topic=13803.msg140843#msg140843 date=1136582407] 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. [/quote]Ahh..your intentions are no longer hidden. [quote author=Mephisto link=topic=13803.msg140839#msg140839 date=1136581289] [quote author=rabbit link=topic=13803.msg140754#msg140754 date=1136520469] The conio library has a getch() function, which does exactly what you want. [/quote] [/quote]Eh..that was before I knew he wanted to catch regardless of focus. [/quote] It wouldn't matter if he wanted it in focus anyways. getch() would require his program to constantly be avaliable to accept user input into a buffer which is certainly not what he wanted anyways, whether the program would be in focus or not. | January 8, 2006, 1:23 AM |