Results 1 to 15 of 15

Thread: [RESOLVED] Sendkeys Postmessage Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    UK
    Posts
    199

    Resolved [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

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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:
    1. Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    2. Declare Function GetForegroundWindow Lib "user32" () As Long
    3. 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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    UK
    Posts
    199

    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?

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Sendkeys Postmessage Problem

    Let me test it out...
    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

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    UK
    Posts
    199

    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

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Sendkeys Postmessage Problem

    Code:
    Const VK_END = &H23
    Const VK_HOME = &H24
    Const VK_CONTROL = &H11
    Const VK_SHIFT = &H10
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    UK
    Posts
    199

    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

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    UK
    Posts
    199

    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?

  12. #12
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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)
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    UK
    Posts
    199

    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

    Code:
    SendKeys "^{End}"

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    UK
    Posts
    199

    Re: Sendkeys Postmessage Problem

    Got It!

    Code:
    Text1.SetFocus
    Text1.SelStart = Len(Text1.Text)

    Thanks Dee-u
    A great help.

  15. #15
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] Sendkeys Postmessage Problem

    Glad to be of help. =)
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width