Results 1 to 4 of 4

Thread: Simulating keyboard

  1. #1

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Simulating keyboard

    What do I best use to simulate keypresses to a window? Right now I'm using sendmessage and findwindowex to send keys to a certain class in a window, but the only thing I can get to work is WM_CHAR. If I want to simulate things like arrow presses, do I have to use keydown and keyup or syskey in succession? Thanks.

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Simulating keyboard

    use keydown, keyup with the virtual keycode:
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    2.                 ByVal hwnd As Long, _
    3.                 ByVal wMsg As Long, _
    4.                 ByVal wParam As Long, _
    5.                 lParam As Any) As Long
    6.  
    7. Private Const WM_KEYDOWN = &H100
    8. Private Const WM_KEYUP = &H101
    9.  
    10. Private Const VK_LEFT = &H25
    11. Private Const VK_UP = &H26
    12. Private Const VK_RIGHT = &H27
    13. Private Const VK_DOWN = &H28
    14.  
    15. Private Sub Command1_Click()
    16.     ' Assuming lhWnd is the handle of the edit box or whatever
    17.     SendMessage lhWnd, WM_KEYDOWN, VK_LEFT, 1&
    18.     SendMessage lhWnd, WM_KEYUP, VK_LEFT, 1&
    19. End Sub
    There's also a keyb_event API, but you can only direct key presses to the app currently in focus

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Simulating keyboard

    Also the SendInput API might be an other alternative. Never tried it much though...


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    Member
    Join Date
    Jun 2006
    Posts
    38

    Re: Simulating keyboard

    Sometimes it is necessary to use Postmessage instead of Sendmessage, as well. Specifically with Vb.net. Don't ask me why.

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