CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2006
    Posts
    32

    A Problem with SendKeys

    Please add the following to the KeyUp (or KeyDown) event of Text1
    Place cursor at the beginning of a word and press F-12. The word will be selected, as expected.
    ' Key Up
    Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    ' F-12
    If KeyCode = 123 Then
    SendKeys "^+{LEFT}"
    End If
    End Sub
    If you break execution manually (Ctrl-Break), the Immediate Window will reveal the selection as made:
    ? Text1.SelText
    ? Text1.SelLength
    Cut, Copy, Del, and other actions will also work.

    However, if you try to peek at the the selection with MsgBox:
    ' Key Up
    Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    ' F-12
    If KeyCode = 123 Then
    SendKeys "^+{LEFT}"
    MsgBox(Text1.SelText)
    End If
    End Sub
    Nothing will be shown as selected!
    Obviously, the selection is made, but not at this point!

    So, what is going on? And, more to the point, how could I trap and use the selection ?

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: A Problem with SendKeys

    Give the system time to select the text. Put a DoEvents in like shown here and it works:
    Code:
    Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
      If KeyCode = 123 Then
         SendKeys "^+{LEFT}"
         DoEvents
         MsgBox (Text1.SelText)
      End If
    
    End Sub

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: A Problem with SendKeys

    TT(n) has an article that explains alternatives to SendKeys that actually work correctly. Search for any of his posts. It's in his signature
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured