Results 1 to 9 of 9

Thread: Keypress ?

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Keypress ?

    How do i get a keypress to simulate ?

    Like i have this old comp games that you need to press 'o' for left and 'p' for right. so i want to be able to click a command button that will press these keys for me alternating in a loop. I also want to be able to increase the speed of this alternation .

    any ideas ?

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Look at SendKeys in the help files.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    Ok thanks.

    Do i need to code it so that the keys will be send to the app that run this game or how does it work ?

  4. #4
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    May want to look into a Macro program also, it can do all that for you.

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Also look for the keyb_event API


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

  6. #6
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Start a form, and put a Timer control on it from the toolbox.
    Set the timer's Enabled property to False, and set the Interval property to 500

    Also put a button on the form, and in the Click event of the button:

    VB Code:
    1. Private Sub Command1_Click()
    2. If Timer1.Enabled = True Then
    3.     Timer1.Enabled = False
    4. Else
    5.     AppActivate "Microsoft Word", True
    6.     Timer1.Enabled = True
    7. End If
    8. End Sub

    I've used Microsoft Word here - but if you put the title of the program (what appears in the title bar of the window) this should work.

    Then, in the Timer event of the timer control:

    VB Code:
    1. Private Sub Timer1_Timer()
    2. Static LastKey As String
    3.  
    4. If LastKey = "O" Then
    5.     SendKeys "P", True
    6.     LastKey = "P"
    7. Else
    8.     SendKeys "O", True
    9.     LastKey = "O"
    10. End If
    11. DoEvents
    12. End Sub

    This will send the O and P keypresses to the activated application (in my case, Microsoft Word). Use the button to switch the timer on or off.

    Vary the Interval property of the timer to speed up / slow down the keypresses. (a smaller number is faster)
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  7. #7

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    Originally posted by Buzby
    Start a form, and put a Timer control on it from the toolbox.
    Set the timer's Enabled property to False, and set the Interval property to 500

    Also put a button on the form, and in the Click event of the button:

    VB Code:
    1. Private Sub Command1_Click()
    2. If Timer1.Enabled = True Then
    3.     Timer1.Enabled = False
    4. Else
    5.     AppActivate "Microsoft Word", True
    6.     Timer1.Enabled = True
    7. End If
    8. End Sub

    I've used Microsoft Word here - but if you put the title of the program (what appears in the title bar of the window) this should work.

    Then, in the Timer event of the timer control:

    VB Code:
    1. Private Sub Timer1_Timer()
    2. Static LastKey As String
    3.  
    4. If LastKey = "O" Then
    5.     SendKeys "P", True
    6.     LastKey = "P"
    7. Else
    8.     SendKeys "O", True
    9.     LastKey = "O"
    10. End If
    11. DoEvents
    12. End Sub

    This will send the O and P keypresses to the activated application (in my case, Microsoft Word). Use the button to switch the timer on or off.

    Vary the Interval property of the timer to speed up / slow down the keypresses. (a smaller number is faster)
    Perfect ! thanks.

  8. #8

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    Ok this doesnt actually server my purpose.

    I want to shell cmd then run a command in it, then when that command has finished running, do something else,

    shellandwait "cmd /k netstat-n"

    shell "shutdown.exe -s"

    but if using shellandwait it waits for cmd to be shut down before i can progress in my code if that makes sense ?

  9. #9
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Try using

    shellandwait "cmd /C netstat-n"

    having a /K in there makes the window stay open after the cmd has finished - putting a /C in there makes the window close.

    I'm confused.. what does this have to do with SendKeys ?
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

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