is there a way using code to simulate the user pressing D in another program besides the app?
Printable View
is there a way using code to simulate the user pressing D in another program besides the app?
Here is one of the possible ways:
VB Code:
Const VK_H = 72 Const VK_E = 69 Const VK_L = 76 Const VK_O = 79 Const KEYEVENTF_EXTENDEDKEY = &H1 Const KEYEVENTF_KEYUP = &H2 Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Private Sub Form_KeyPress(KeyAscii As Integer) 'Print the key on the form Me.Print Chr$(KeyAscii); End Sub Private Sub Form_Paint() 'Clear the form Me.Cls keybd_event VK_H, 0, 0, 0 ' press H keybd_event VK_H, 0, KEYEVENTF_KEYUP, 0 ' release H keybd_event VK_E, 0, 0, 0 ' press E keybd_event VK_E, 0, KEYEVENTF_KEYUP, 0 ' release E keybd_event VK_L, 0, 0, 0 ' press L keybd_event VK_L, 0, KEYEVENTF_KEYUP, 0 ' release L keybd_event VK_L, 0, 0, 0 ' press L keybd_event VK_L, 0, KEYEVENTF_KEYUP, 0 ' release L keybd_event VK_O, 0, 0, 0 ' press O keybd_event VK_O, 0, KEYEVENTF_KEYUP, 0 ' release O End Sub
It is for other letters but just change them. ;)
Almost forgot. The contant for D is 68.
Try this:
VB Code:
Private Declare Function PostMessage Lib "user32" _ Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long Private Const WM_CHAR = &H102 Private Sub Command1_Click() PostMessage hwnd_of_app, WM_CHAR, vbKeyD, 0 'send D key End Sub