Re: Sending Keystrokes to application with known hwnd
- From: MiReK <rekked@xxxxxxxxxxxxxx>
- Date: Thu, 11 Aug 2005 13:26:12 +0200
William napisał(a):
Amir.
Below is an example I wrote for a friend of mine, from an example I found on the net. Put this code in a form with a command button named "Command1". This will fail if the program 'Notepad' is not running, or the title 'Untitled - Notepad' can't be found.
Option Explicit
' Message to tell SendInput that we want to send keyboard input Private Const INPUT_KEYBOARD = 1
' Message to tell SendInput that the key that was "presses" Private Const KEYEVENTF_KEYDOWN = 0&
' Message to tell SendInput that the key that was "presses" is now to be released Private Const KEYEVENTF_KEYUP = &H2
' The virtual key code for the F1 key. Private Const VK_F1 = &H70
' A window receives this message when the user chooses a command from the window menu ' (formerly known as the system or control menu) or when the user chooses the maximize ' button, minimize button, restore button, or close button. Private Const WM_SYSCOMMAND = &H112
' Maximizes the window. Private Const SC_MAXIMIZE = &HF030&
' Structure to hold information about the input Private Type GENERALINPUT dwType As Long xi(0 To 23) As Byte End Type
' Structure to hold specific information about the keyboard input Private Type KEYBDINPUT wVk As Integer wScan As Integer dwFlags As Long time As Long dwExtraInfo As Long End Type
Private Declare Function SendInput Lib "user32.dll" ( _ ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _ Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _ ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _ ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Command1_Click()
' Local variable(s) Dim hwnd As Long Dim KI As KEYBDINPUT Dim GI(0 To 1) As GENERALINPUT
' Try to find the window. hwnd = FindWindow(CLng(0), "Untitled - Notepad")
' If the window can't be found. If hwnd = 0 Then ' Exit this procedure Exit Sub End If
' Send a message to bring the window to the front. Call SetForegroundWindow(hwnd)
' Send a message to maximize the window. Call SendMessage(hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, ByVal vbNullString)
With KI ' The key down flag. .dwFlags = KEYEVENTF_KEYDOWN ' Set the virtual F1 function key. .wVk = VK_F1 End With
' Set the type of message to keyboard input. GI(0).dwType = INPUT_KEYBOARD
' Place the keyboard data in the GI structure variable. CopyMemory GI(0).xi(0), KI, Len(KI)
With KI ' The key up flag. .dwFlags = KEYEVENTF_KEYUP ' Set the virtual F1 function key. .wVk = VK_F1 End With
' Set the type of message to keyboard input. GI(1).dwType = INPUT_KEYBOARD
' Place the keyboard data in the GI structure variable. CopyMemory GI(1).xi(0), KI, Len(KI)
' Send the input. ' The number 2 indicates the number of messages that we are sending. (GI(0) and GI(1)) Call SendInput(2, GI(0), Len(GI(0)))
End Sub
William
DOes it work in delphi? Will this commands work in delphi environment? .
- References:
- Sending Keystrokes to application with known hwnd
- From: Amir
- Re: Sending Keystrokes to application with known hwnd
- From: William
- Sending Keystrokes to application with known hwnd
- Prev by Date: Re: SetParent: When toggling on and off, how do I keep the form stationary?
- Next by Date: API function to remove Write Protection of a file
- Previous by thread: Re: Sending Keystrokes to application with known hwnd
- Next by thread: Re: Sending Keystrokes to application with known hwnd
- Index(es):