|
-
Aug 8th, 1999, 12:15 PM
#1
Thread Starter
Junior Member
there's a vb function SendKeys that will send a string of keypresses to an active application. is there an equivalent to this function in the windows api?
-
Aug 8th, 1999, 12:35 PM
#2
Hyperactive Member
I remember that there was a function, but i can't remember which one.. (and i might be wrong...) but SendMessage with the WM_KEYDOWN message should do the trick for you
MSDN says "The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed. "
-
Aug 8th, 1999, 12:57 PM
#3
Thread Starter
Junior Member
tried that, didn't work. here's my code:
t = FindWindow(vbNullString, "calculator")
SendMessage t, WM_KEYDOWN, VK_NUMPAD3, 1
SendMessage t, WM_KEYUP, VK_NUMPAD3, 0
i have declared everything properly, there is a calculator window open for testing purposes. no key is sent. any ideas?
-
Dec 11th, 1999, 09:16 AM
#4
Junior Member
dude it doesn't matter if its "c" or "C" API doesn't really care.
-
Dec 11th, 1999, 09:26 AM
#5
Here is a simple example. Note that it doesn't set the shift state, nor does it check for other key combinations like ctrl or alt. Also sending a chr(13) to a dos box only succeedes if the dos box has a window, so if it is set to full screen, it does not receive chr(13).
Code:
Option Explicit
Private Declare Function CharToOem Lib "user32" Alias "CharToOemA" (ByVal lpszSrc As String, ByVal lpszDst As String) As Long
Private Declare Function OemKeyScan Lib "user32" (ByVal wOemChar As Integer) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Integer
Private Const KEYEVENTF_KEYUP = &H2
'
' Sends a single character using keybd_event
' Note that this function does not set shift state
' (By pressing down the shift key or setting the shift keys state)
' and it doesn't handle extended keys.
'
Private Sub SendAKey(ByVal Karakter As String)
Dim vk As Integer
Dim scan As Integer
Dim oemchar As String
Dim dl As Long
' Get the virtual key code for this character
vk = VkKeyScan(Asc(Karakter)) And &HFF
oemchar = " " ' 2 character buffer
' Get the OEM character - preinitialize the buffer
CharToOem Left$(Karakter, 1), oemchar
' Get the scan code for this key
scan = OemKeyScan(Asc(oemchar)) And &HFF
' Send the key down
keybd_event vk, scan, 0, 0
' Send the key up
keybd_event vk, scan, KEYEVENTF_KEYUP, 0
End Sub
Public Sub MySendKeys(ByVal sText As String)
Dim Cntr As Long
For Cntr = 1 To Len(sText)
SendAKey Mid$(sText, Cntr)
Next Cntr
End Sub
Private Sub Command1_Click()
AppActivate "MyFormCaption"
Call MySendKeys("Hello World")
End Sub
-
Dec 11th, 1999, 09:56 AM
#6
Thread Starter
Junior Member
RoundCow was right - you don't need the capital C - the API doesn't care.
Frans' method (or at least keybd_event) works.. the SendMessage function doesn't. Thanks Frans for working it out...
is there a way to set the shift state with keybd_event? you can't use the Or keyword to mix vk_a and vk_shift for example.. and is there a way (using API of course) to send characters like [ and ] and { etc etc?
-
Dec 11th, 1999, 10:28 AM
#7
It seems I worked this out before. I got too many projects in my sample directory 
I hope I didn't forget something in combining them.
Add the following declarations to the rest of them:
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Const VK_SHIFT = &H10
And replace the SendAKey sub by this one:
Code:
Private Sub SendAKey(ByVal Karakter As String)
Dim vk As Integer
Dim scan As Integer
Dim oemchar As String
Dim dl As Long
Dim Shift As Boolean
Dim Shiftscan As Long
Dim Char As Long
' Get the virtual key code for this character
vk = VkKeyScan(Asc(Karakter)) And &HFF
oemchar = " " ' 2 character buffer
' Get the OEM character - preinitialize the buffer
Char = OemKeyScan(Asc(oemchar))
' Get the scan code for this key
scan = Char And &HFF
Shift = Char And &H20000
If Shift Then
Shiftscan = MapVirtualKey(VK_SHIFT, 0)
keybd_event VK_SHIFT, Shiftscan, 0, 0
End If
' Send the key down
keybd_event vk, scan, 0, 0
' Send the key up
keybd_event vk, scan, KEYEVENTF_KEYUP, 0
If Shift Then
keybd_event VK_SHIFT, Shiftscan, KEYEVENTF_KEYUP, 0
End If
End Sub
-
Dec 12th, 1999, 08:26 AM
#8
RoundCow is right. The API doesn't care, I was using Spy++ which does care.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|