Friday, December 04, 2009

How to emulate F9 key

Sometimes F9 makes good staff for us, so here is an example (I really don't remember where I got it).

'F9
Declare Function VkKeyScan Lib "User32.dll" Alias "VkKeyScanA" ( Byval char As Integer ) As Integer
Declare Function MapVirtualKey Lib "User32.dll" Alias "MapVirtualKeyA" ( Byval wCode As Long, Byval wMapType As Long ) As Long
Declare Sub keybd_event Lib "User32.dll" ( Byval Virtual As Integer, Byval OEMScan As Integer, Byval Flags As Long, Byval ExtrInfo As Long )
Const KE_KEYDOWN& = 0
Const KE_KEYUP& = 2 

Sub F9
  On Error Goto errorproc

  Dim vk As Long
  Dim sc As Integer

  vk = &H78

  sc = Cint( MapVirtualKey( vk&, 0 ) )

  Call keybd_event( Cint( vk& ), sc%, KE_KEYDOWN&, 0 )
  Yield
  Call keybd_event( Cint( vk& ), sc%, KE_KEYUP&, 0 )
  Yield

  endofsub:
    Exit Sub
  errorproc:
    Msgbox "Error #" & Err & " on line " & Erl & " in function " & Lsi_info(2) & " : " & Error, 48, "Runtime error"
  Resume endofsub 
End Sub

No comments :