Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Windows 10 with Visual Studio 2015 net 4.6 and coding in vb and wpf.
Existing code of keybd_event of user32.dll works fine in visual studio 2010.

ASM
<DllImport("user32.dll")>
 Shared Sub keybd_event(bVk As Byte, bScan As Byte, dwFlags As UInteger, dwExtraInfo As Integer)
 End Sub




After installing Community 2015, the code analyser says
Severity Code Description Project File Line
Warning CA1901 As it is declared in your code, parameter 'dwExtraInfo' of P/Invoke 'NativeMethods.keybd_event(Byte, Byte, UInteger, Integer)' will be 4 bytes wide on 64-bit platforms. This is not correct, as the actual native declaration of this API indicates it should be 8 bytes wide on 64-bit platforms. Consult the MSDN Platform SDK documentation for help determining what data type should be used instead of 'Integer'.

I searched web for solution. I could not get or unable to understand with my existing level of knowledge. I code only for my purposes and I am a novice.
The keybd_event is no longer used and in place SendInput is used. I tried msdn forums and the code does not work.

Question is:
1. What should I use keybd_event or SendInput
2. In case keybd_event is to be used how to rectify the warning as above.
3. if SendInput to be used, I request assistance in getting the code for the purpose.
c# code example also will do.
Thanks
Posted

1 solution

  1. Don't use keybd_event. This function has been superseded by SendInput. See the comment here:
    https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^]
  2. Inapplicable; don't use it.
  3. All you need is here: http://www.pinvoke.net/default.aspx/user32.sendinput[^].
    The request for code sample makes little sense, because you did not tell us what you want to achieve. But the usage is quite trivial, and the function is very reliable; I don't remembers if it presented any problems.


—SA
 
Share this answer
 
Comments
Sesharaman 15-Aug-15 23:46pm    
Thank You for posting at the earliest.
It works fine for keys except CapsLock using sendinput. For capslock key, if I include keydown structure , it is always on/off depending on the initial state of Off/On. When I use both keydown and keyup structures and send it sendinput, the capslock key returns to original position and is not toggled(rather double toggled). Any help here.
Sesharaman 16-Aug-15 0:03am    
Mr.Sergey Alexandrovich Kryukov,
I created a separate function for capslock key toggle and used the sendinput. Now it works fine. Thanks
Sergey Alexandrovich Kryukov 16-Aug-15 9:44am    
Great. You are very welcome.
—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