Results 1 to 10 of 10

Thread: Help with Sendkeys command

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    3

    Help with Sendkeys command

    this is probably a n00b question
    I want to have a program , that after clicking a button on a form (could be called "Go") repeatedly simulates the pressing of a key on the keyboard until it is told to stop.

    I can work it all out apart from how to code the keyboard press.

    Can anyone help please
    Thanks
    Last edited by paulrg; Nov 24th, 2006 at 11:57 AM. Reason: Changed topic as per request

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Please help

    Sendkeys("a")

    that would send the a letter to whatever app was in focus
    Chris

  3. #3
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Please help

    Welcome to VBF Paul.

    Please give your topics a useful title, everyone needs help.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Please help

    Name your CommandButton "cmdGo" and try this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     cmdGo.Caption = "Go"
    5. End Sub
    6.  
    7. Private Sub cmdGo_Click()
    8.     If cmdGo.Caption = "Go" Then
    9.         cmdGo.Caption = "Stop"
    10.     Else
    11.         cmdGo.Caption = "Go"
    12.     End If
    13.         Do Until cmdGo.Caption = "Go"
    14.             SendKeys "a", True
    15.                 DoEvents
    16.         Loop
    17. End Sub
    And welcome to VBForums

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Please help

    Quote Originally Posted by gavio
    Name your CommandButton "cmdGo" and try this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     cmdGo.Caption = "Go"
    5. End Sub
    6.  
    7. Private Sub cmdGo_Click()
    8.     If cmdGo.Caption = "Go" Then
    9.         cmdGo.Caption = "Stop"
    10.     Else
    11.         cmdGo.Caption = "Go"
    12.     End If
    13.         Do Until cmdGo.Caption = "Go"
    14.             SendKeys "a", True
    15.                 DoEvents
    16.         Loop
    17. End Sub
    And welcome to VBForums
    Wow the code I was going to post was exactly the same as yours.

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Please help

    Quote Originally Posted by DigiRev
    Wow the code I was going to post was exactly the same as yours.


    I found this peace of code to be... anoying... my notebook ventilator just sang like a canarian...

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    3

    Re: Please help

    thats excellent , just what I was after
    thanks guys

  8. #8
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Please help

    you can use a timer to provide an interval after you click go, because you probably dont want to send key presses to your form or button
    Chris

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    3

    Re: Please help

    Yes ive used a timer to do this

    like this

    Private Sub Timer1_Timer()
    SendKeys "4"
    End Sub

    The program works fine when I run it and then load up notepad. But when I try to run the game im trying to get this to run with it doesnt work , almost as if the game doesnt have focus.

    Anyone have any thoughts?

    Great stuff so far though

  10. #10
    New Member
    Join Date
    Nov 2006
    Posts
    10

    Re: Help with Sendkeys command

    Whats the game?

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