CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2001
    Posts
    4

    Emulate a pressed key

    How do I make a funcion key, F1 or so on, be pressed programmatically in Visual Basic? Also, how do execute a dos program in Visual Basic?


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Emulate a pressed key

    'if you need to execute some code on F1 press, put the same code into some 'procedure and then call this proc when you want to execute it.

    'Here how you capture F1 and Return on Key down event

    Public Const KEYEVENTF_EXTENDEDKEY = &H1
    Public Const KEYEVENTF_KEYUP = &H2
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags
    As Long, ByVal dwExtraInfo As Long)

    public sub KeyDown()

    keybd_event vk_return,0,0,0 'return key down
    keybd_event vk_return,0,keyeventf_keyup,0 'return key up

    end sub

    here it is with F1
    public sub KeyDown()

    keybd_event vk_F1,0,0,0 'F1 key down
    keybd_event vk_F1,0,keyeventf_keyup,0 'F1 key up

    end sub

    'how do execute a dos program in Visual Basic?

    dim RetVal

    RetVal = Shell("c:\yourprog.exe")





    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    78

    Re: Emulate a pressed key

    The code presented gives the following compilation error:
    "Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules"

    Typo? By the way, is there a way to accomplish the same thing without calling API?


  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Emulate a pressed key

    Now, add a .bas module to your project to use Iouri code, and put constants and declarations there
    Or declare all pribìvate and put them at top of form where you want to use the code
    Another way to play with keys is:
    'one command button on one form
    'set the form keypreview property to true or it will not work properly!
    Option Explicit

    Private Sub Command1_Click()
    If Me.WindowState <> vbMinimized Then
    Me.SetFocus
    SendKeys "{F2}", True 'send a keystroke to this form
    End If
    End Sub

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF1 Then
    Call Form_KeyDown(vbKeyF2, 0)
    End If
    If KeyCode = vbKeyF2 Then
    'do your stuff
    If Me.WindowState <> vbMinimized Then
    Me.SetFocus
    SendKeys "{F3}", True 'send a keystroke to this form
    End If
    End If
    End Sub

    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    Print KeyCode
    End Sub


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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