?
Solved

Cathing keystrokes(/interrupts) from system.

Posted on 2003-03-23
11
Medium Priority
?
376 Views
Last Modified: 2010-04-01
Well, hello guys.

I have made a program (in Visual C++), and now i want it to react on userinput, not nessesarily given to the program. I mean, if the program runs in the background, then it should hook up on some interrupt (or something like that) so when the user hits some shortcut (e.g 'F10'), then the program should see this, and then do some work. I think it would be something like using the "signal(..)" in C, but it should catch keystrokes.... Know what i mean?

/Jim
0
Comment
Question by:jim_s_w
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 

Expert Comment

by:CWay
ID: 8190612
If you're running Win32 try out the API function GetAsyncKeyState(int key), which gives the status of the given key. As the parameter you can even pass 1 for the left mouse button, for example. You can also look for combined key hits like SHIFT+F1 by calling the function twice with the different key codes (VK_LSHIFT and VK_F1). GetAsyncKeyState works independently of which thread has the keyboard focus.
0
 

Author Comment

by:jim_s_w
ID: 8191025
Well, i tried it, and it works ok. But then i have to call 'GetAsyncKeyState' over and over. Is that applicable? I mean is that the way applications catch keystrokes, by continuisly checking for specific keys pressed (maybe with a short sleep between calls)? I was thinking about some way to tell Windows, "when any key is pressed, call this function at this address". Much like 'signal' in C. But i think that 'signal' in C only catched "Ctrl+C", "kill" and similar?
0
 
LVL 12

Expert Comment

by:Salte
ID: 8191195
What you want to do is wait for some event to happen.

Therefore you must use one of the WaitForSingleObject() or related functions (WaitForMultipleObjects() for example).

Your problem is that the keyboard isn't automatically something you can wait on, if you just waited for stdin you would wait until your program got the focus etc which would never happen. In other words you need to make sure you wait for keyboard input - even if it isn't to your program.

How you get a handle to that I don't know you probably have to dig some but I am pretty sure it is the WaitFor... functions you must use when you find the correct handle to wait for.

One way to do it would be to capture the input focus, wait for key and as long as it isn't your key (F10 for example) you would send it on to whoever had the focus. This would be troublesome though. I see two problems with this:

1. The user would see graphically (visually) that the program he is working on does not have focus - this would probably seem unsatisfying to the user in that he would never completely know if his program had the focus or not since literally it would be your program having the focus at all times.

2. What if the key you chose was the key the user wanted to send to whatever program did have the input focus. If you chose F10, what if he wanted to press F10 and send it to the program he is using?

I know you have the idea from the old TSR's which often worked that way but that was typically in a non-windowing environment. With windows it simply isn't necessary to have applications that does that. If I want to activate your application I just start it and it pops up in another window and when I want to activate your application I ALT+TAB or click on the task bar to get it active. No need for a F10 or whatever to activate any application.

I would suggest you just drop the project and rather let people activate your program the usual way unless you have very compelling reasons for require such a 'hot key'.

Alf
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:bkrahmer
ID: 8192676
If you look back a couple weeks ago, you will see the same question asked, with several better answers.

cheers,
brian
0
 

Author Comment

by:jim_s_w
ID: 8197501
bkrahmer-> If you refer to 'Q_20554106' it talks about DOS environment, and not WIN32.

Salte-> I have checked the WaitFor...Object and they can not wait for key input. Only file changes, and events from specific threads and processes.

Other suggestions?
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8200019
No, That probably wasn't it.  There were specific windows API's mentioned.  It may have been in the windows programming section or something like that.  I think the solution you are after is a kernel hook via a device driver.

brian
0
 
LVL 12

Expert Comment

by:Salte
ID: 8201795
Actually, they can wait for any event provided you have a handle that can be 'signaled'.

If you have a handle to the keyboard you can use WaitFor on that handle.

The problem is to get a handle to the keyboard and to ensure that that handle will be signaled when the keyboard input arrives.

Absolutely ALL waiting in Win32 is done using some form of WaitFor... Even select() is implemented using WaitForMultipleObjects() in Win32.

To the OS the keyboard is a 'file'.

Alf
0
 

Accepted Solution

by:
jim_s_w earned 0 total points
ID: 8203949
I found an answer at Q_20453601.

Well weard i have to create a DLL, because the 'GetAsyncKeyState' works from inside the exe, and can tell if a key have been pressed, also if the key have been pressed outside the program. So i think it should be possible to do it without using a DLL.

I think i'll look more into the WaitForSingleObject(), but the next step is then to get the keyboardinput as a stream(file). Anuone knows how to do that?
0
 
LVL 12

Expert Comment

by:Salte
ID: 8203983
I would assume that CreateFileEx() would do it, but you have to provide the filename argument that indicate the keyboard driver or some such. Also, check what command codes that driver has to make it signal the handle when a key has entered etc...

Alf
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9544060
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Archived (PAQ) with a refund

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question