1. Call Getasynckeystate () to get the state of the specified key, Getactivekey () retrieves the key state in the specified range
2. Call keybd_event () to synthesize a keystroke event, usually between two keystroke events you need to set a time interval
3. Call Mapvirtualkey () to get the hardware scan code for the specified key, and the second parameter to the keybd_event () is valid for DirectInput.
Public ClassImitatekeyclassPrivate Declare SubKeybd_eventLib "User32"(ByValBvk as Byte,ByValBscan as Byte,ByValDwFlags as Integer,ByValdwExtraInfo as Integer) Private Declare FunctionMapvirtualkeyLib "User32" Alias "Mapvirtualkeya"(ByValWcode as Integer,ByValWmaptype as Integer) as Integer Private Declare FunctionGetasynckeystateLib "User32"(ByValVkey as Integer) as Integer " " <summary> " "send button" " </summary> " " <param name= "Chararray" ></param> Public SubSendstring (Chararray as String) for eachSubchar as Char inchChararray keybd_event (ASC(Subchar), Mapvirtualkey (ASC(Subchar),0), &H1Or 0,0)'Press theSystem.Threading.Thread.Sleep (Ten)'DelayKeybd_event (ASC(Subchar), Mapvirtualkey (ASC(Subchar),0), &H1Or&H2,0)'pop UpSystem.Threading.Thread.Sleep (Ten) Next End Sub " " <summary> " "gets the key state of the A~z" " </summary> " " <returns></returns> Public FunctionGetactivekey () as Byte fori = $ to - 'A~z's ASICC code IfCurrentkeystate (i) =True Then ReturnINext Return 0 End Function " " <summary> " "get keyboard key State" " </summary> " " <param name= "KeyCode" ></param> " " <returns></returns> Private FunctionCurrentkeystate (ByValKeyCode as Byte) as Boolean DimTemp as Integer=getasynckeystate (keycode)Return(IIf(temp =-32767,True,False)) End FunctionEnd Class
[vb.net] Call API Get/SET keyboard key state