|
-
Oct 28th, 2009, 02:16 PM
#1
Thread Starter
Addicted Member
[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
-
Oct 28th, 2009, 02:25 PM
#2
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
Last edited by Siddharth Rout; Oct 28th, 2009 at 02:29 PM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Oct 28th, 2009, 02:37 PM
#3
Thread Starter
Addicted Member
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?
-
Oct 28th, 2009, 03:41 PM
#4
Re: Sendkeys Postmessage Problem
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Oct 28th, 2009, 03:46 PM
#5
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
-
Oct 28th, 2009, 03:50 PM
#6
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Oct 28th, 2009, 03:54 PM
#7
Thread Starter
Addicted Member
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
-
Oct 28th, 2009, 03:57 PM
#8
Re: Sendkeys Postmessage Problem
Code:
Const VK_END = &H23
Const VK_HOME = &H24
Const VK_CONTROL = &H11
Const VK_SHIFT = &H10
-
Oct 28th, 2009, 03:57 PM
#9
Re: Sendkeys Postmessage Problem
Did you try the code in post 6
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Oct 28th, 2009, 04:14 PM
#10
Thread Starter
Addicted Member
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
-
Oct 28th, 2009, 04:15 PM
#11
Thread Starter
Addicted Member
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?
-
Oct 28th, 2009, 04:37 PM
#12
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)
-
Oct 28th, 2009, 04:49 PM
#13
Thread Starter
Addicted Member
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
-
Oct 28th, 2009, 05:14 PM
#14
Thread Starter
Addicted Member
Re: Sendkeys Postmessage Problem
Got It!
Code:
Text1.SetFocus
Text1.SelStart = Len(Text1.Text)
Thanks Dee-u
A great help.
-
Oct 30th, 2009, 01:18 AM
#15
Re: [RESOLVED] Sendkeys Postmessage Problem
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
|