Click here to Skip to main content
15,884,039 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
[DllImport("user32.dll")]
       static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);


I'm writing a Test tool in C# (.Net 4) and it generates Key events using the above API wrapper. I'm using this API for the purpose of creating KeyUp\KeyDown events.

The API works with nearly all applications, but I have found it doesn't work with a few applications. Those applications do not respond to events recreated by keybd_event; the key events are totally ignored.

However, if I use AutoHotKeys, those applications do respond to Key event generated by AHK scripts.

Any educated guesses as to why a few applications don't respond to events created by Keybd_ events, and what other solutions I might uses to generate Key events? Or maybe I'm not passing the the correct parms to the this API, thus key events are ignored by some apps.

Thanks
Posted
Updated 4-Sep-11 7:50am
v2

You didn't mentioned the applications that are unable to respond to the API. In any case take a look at the following link. It shows clearly how to work with keybe_event API http://pinvoke.net/default.aspx/user32.keybd_event[^]
 
Share this answer
 
v2
Comments
nrnoble 4-Sep-11 19:36pm    
Thanks.... I have looked over the pinvoke options and not seeing anything that's would alter the results.

Keybd_ events works fine with standard Windows applications, but its game engines that are not responding the events created by the API Keybd_ event.
Wonde Tadesse 6-Dec-11 11:20am    
You 're welcome
This API is low-level; so applications do not respond to it directly. The function is called by interrupt handler. Maybe you are generating not exactly same input which you think you need for this application to respond. Also, this API is currently superseded by the function SendInput which I recommend to use instead of keybd_event.

Please see http://msdn.microsoft.com/en-us/library/ms646310%28v=vs.85%29.aspx[^].

[EDIT — in respond to OP's comment about direct access to hardware]

It's not easy to tap to hardware directly from the application; if it's even possible without having a kernel-mode (only) custom driver installed, it would suggest OS has a huge security hole. I would rather suggest the problem in your application.
Here is what you can do: create a clear code sample which manifest this problem. You should try to scale down your code to obtain something really minimal, essentially an application skeleton.

Chances are: 1) you will find a problem down the road and fix it; 2) a problem disappears in the skeleton; you will retrace your steps and find out where you added a problem, then the problem will be reduced to case #1 or; 3) your skeleton will not give you a clue but it will be so small that you could post in to CodeProject; in this case, don't create a new page; use "Improve question" on this page; if it happens, please notify me as well by adding a comment to this post — let's see.

—SA
 
Share this answer
 
v2
Comments
nrnoble 5-Sep-11 17:47pm    
Thanks for the info and the link. This is becoming one of those type of problems where I learn a lot good technical info, but none of it solves the problem. I probably will use SendInput for other projects, but Key events it generates are also being ignored by a few game engines. I almost would think they were tapping the hardware directlty, bypassing the OS, but AutoHotKeys is able to generate key events that are not ignored.
Sergey Alexandrovich Kryukov 14-Sep-11 18:52pm    
So now I suggest some more steps; please see my updated solution, after [EDIT].
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900