Advertisement
Guest User

Windows API send keypress CtrlF10 to control

a guest
Apr 11th, 2013
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.54 KB | None | 0 0
  1. ' Windows API
  2. ' code to send keypress to control..
  3. '
  4. ' emulates AutoIT ControlSend("WindowText", "", "[CLASS:... INSTANCE=1]", "^{F10}")
  5. <DllImport("user32.dll")> _
  6.     Private Shared Function GetKeyboardState(ByVal keyState() As Byte) As Boolean
  7.     End Function
  8.  
  9.     <DllImport("user32.dll")> _
  10.     Private Shared Function SetKeyboardState(lpKeyState() As Byte) As Boolean
  11.     End Function
  12.  
  13.     <DllImport("user32.dll")> _
  14.     Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
  15.     End Function
  16.  
  17.     <DllImport("user32.dll")> Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
  18.     End Function
  19.  
  20.     <DllImport("kernel32.dll")> _
  21.     Public Shared Function GetCurrentThreadId() As Integer
  22.     End Function
  23.  
  24.     <DllImport("user32.dll", SetLastError:=True)> _
  25.     Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, _
  26.                           ByRef lpdwProcessId As Integer) As Integer
  27.     End Function
  28.  
  29.     <DllImport("user32.dll")> _
  30.     Public Shared Function AttachThreadInput(ByVal idAttach As System.UInt32, ByVal idAttachTo As System.UInt32, ByVal fAttach As Boolean) As Boolean
  31.     End Function
  32.  
  33. 'Emulates WinAttach AutoIt procedure
  34. Private Sub WinAttach(hwnd As IntPtr, bAttach As Boolean)
  35.         Dim myThread As Integer
  36.         Dim newThread As Integer
  37.         Dim curThread As Integer
  38.         myThread = GetCurrentThreadId()
  39.  
  40.         If bAttach = True Then
  41.             curThread = GetWindowThreadProcessId(hwnd, Nothing)
  42.             AttachThreadInput(myThread, curThread, True)
  43.             newThread = GetWindowThreadProcessId(hwnd, Nothing)
  44.             AttachThreadInput(curThread, newThread, True)
  45.             AttachThreadInput(myThread, newThread, True)
  46.         Else
  47.             AttachThreadInput(myThread, newThread, False)
  48.             AttachThreadInput(curThread, newThread, False)
  49.             AttachThreadInput(myThread, curThread, False)
  50.  
  51.         End If
  52.  
  53. End Sub
  54.  
  55. Private Sub EmulateCTRLF10()
  56.  
  57.  Dim kbState(255) As Byte
  58.         GetKeyboardState(kbState)
  59.         kbState(17) = &H80 //Set CTRL as pressed
  60.         SetKeyboardState(kbState)
  61.        
  62.         PostMessage(hWnd, &H100, 17, 1900545) 'press ctrl
  63.         PostMessage(hWnd, &H100, 121, 4456449) 'press f10
  64.  
  65.         PostMessage(hWnd, &H101, 121, &HC0440001) 'unpress f10
  66.         PostMessage(hWnd, &H101, 17, &HC01D0001) 'unpress ctrl
  67. End Sub
  68.  
  69. ' The sub EmulateCTRLF10 must be started in another thread.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement