[RESOLVED] Sendkeys Postmessage Problem
Hi Guys
Im stuck and need some help please.
I have an application which is not working on Vista due to (I believe) the sendkeys statement I have used.
I can sucessfully send a string to a textbox using the Postmessage API call, but how on earth do I send a Ctrl + End, Home + End and a VBCR???
Ive searched and searched, but I am unable to find the answer.
Thanks in advance.
Paul
Re: Sendkeys Postmessage Problem
Remember that SendKeys can only send keystrokes to the active application.
Active application is the one which is in focus to accept keyboard input. To make any Windows active from another application we have to take help from the Windows native API SetForegroundWindow. SetForegroundWindow requires the Windows handle to bring it to the front(Process.MainWindowHandle).
Do a search in the forum. You will find pleant of examples on SetForegroundWindow API
Also see this link "To send a keystroke to a different application"
You will have to use these 3 functions...
vb Code:
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetForegroundWindow Lib "user32" () As Long
Declare Function GetTickCount Lib "kernel32" () As Long
BTW
SendKeys "^{END}" for Ctrl + End
Re: Sendkeys Postmessage Problem
Thanks for the quick response.
I know how to use the sendkeys statement and have used all the above key combinations sucessfully in the already deployed application.
I need to change these statements to an API call - I presume Postmessage? - to cope with Vista.
The textbox to put the string into is on one of my forms and has the focus.
Code:
'Declared in a module
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 vkCommand1_Click()
MsgBox Me.Text1.hwnd
SendKeysExtra "Test Message", Me.Text1.hwnd
End Sub
'This bit is in a module to be re-used as necessary
Public Sub SendKeysExtra(strMsgToSend As String, TextBoxHWND As Long)
Dim lngReturn As Long
For I = 1 To Len(strMsgToSend)
lngReturn = PostMessage(TextBoxHWND, WM_CHAR, Asc(Mid(strMsgToSend, I, 1)), 0&)
Next
End Sub
Does this make sense?
Re: Sendkeys Postmessage Problem
Re: Sendkeys Postmessage Problem
Why do you even need an API to set a text to one of your textboxes? Instead of "SendKeysExtra "Test Message", Me.Text1.hwnd" can you not do "Me.Text1.Text = "Test Message"?
To emulate the sendkeys I am using the keybd_event API, here is for the tab and reverse of tab, you may be able to modify it to suit your needs.
Code:
Public Sub PressTab()
keybd_event VK_TAB, 0, 0, 0 ' press tab
keybd_event VK_TAB, 0, KEYEVENTF_KEYUP, 0 ' release tab
End Sub
Public Sub PressReverseTab()
keybd_event VK_SHIFT, 0, 0, 0 ' press Shift
keybd_event VK_TAB, 0, 0, 0 ' press tab
keybd_event VK_TAB, 0, KEYEVENTF_KEYUP, 0 ' release tab
keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0 ' release Shift
End Sub
Re: Sendkeys Postmessage Problem
Try this
Code:
Dim StrSample As String
For I = 1 To Len(strMsgToSend)
StrSample = Mid(strMsgToSend, I, 1)
lngReturn = PostMessage(TextBoxHWND, WM_CHAR, Asc(StrSample), 1&)
Next
Re: Sendkeys Postmessage Problem
Hi Dee-u
I dont actually want to set the string to the textbox, I was just checking I could write to it using API (of which Im really not very good at).
The problem is I have some CTRL + HOME, CTRL + SHIFT + END type statements using Sendkeys to highlight text in a textbox when the user incorrectly inputs a value.
The sendkeys method doesn't work under Vista.
Your keyboard events seem to be the answer, but what are the values for the CTRL key, HOME key and the END key?
Thanks very much. This is really annoying me now, but I think I may be heading towards a solution.
Paul
Re: Sendkeys Postmessage Problem
Code:
Const VK_END = &H23
Const VK_HOME = &H24
Const VK_CONTROL = &H11
Const VK_SHIFT = &H10
Re: Sendkeys Postmessage Problem
Did you try the code in post 6
Re: Sendkeys Postmessage Problem
Dee-u
I have added the declaration and added 1 button and 1 textbox to the form.
Nothing happens to the textbox.
Any idea why???
Code:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Sub Command1_Click()
Me.Text1.SetFocus
keybd_event VK_CONTROL, 0, 0, 0 ' press ctrl
keybd_event VK_HOME, 0, 0, 0 ' press home
keybd_event VK_HOME, 0, KEYEVENTF_KEYUP, 0 ' release home
keybd_event VK_SHIFT, 0, 0, 0 ' press shift
keybd_event VK_END, 0, 0, 0 ' press end
keybd_event VK_END, 0, KEYEVENTF_KEYUP, 0 ' release end
keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0 ' release shift
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0 ' release ctrl
End Sub
Re: Sendkeys Postmessage Problem
Koolsid
I think Dee-u solution is the way to go, but have you anything to add regarding my previous post?
Re: Sendkeys Postmessage Problem
If you want to select the text of textbox then why not use this?
Code:
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Re: Sendkeys Postmessage Problem
Dee-u your great.
I had completely overlooked that option. That has cleared up 2 of my sendkeys. Just this 1 left...
To set the cursor to the end of the text in the textbox - without highlighting
Re: Sendkeys Postmessage Problem
Got It!
Code:
Text1.SetFocus
Text1.SelStart = Len(Text1.Text)
Thanks Dee-u
A great help.
Re: [RESOLVED] Sendkeys Postmessage Problem