Results 1 to 5 of 5

Thread: [RESOLVED] Function Keys

  1. #1
    Addicted Member
    Join Date
    Jul 11
    Posts
    214

    Resolved [RESOLVED] Function Keys

    hi friends ,in order my application more friendly and let user use keyboard instead of mouse
    I used below code;

    Private Sub Form_KeyPress(KeyAscii As Integer)

    If KeyAscii = 97 Or KeyAscii = 65 Then
    Button1_Click
    End If
    end sub

    so when user presses "a" or "A" it's like as if he has clicked on button1.this piece of code works well,now my question is how can I replace "a" with a function key as

    F1 for example?it seems they dont have it (or they do??)


    thank you so much

  2. #2
    Frenzied Member
    Join Date
    Aug 11
    Location
    B.C., Canada
    Posts
    1,838

    Re: Function Keys

    you could use api instead

    Code:
    Option Explicit
    
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
      Private Const VK_F1 As Long = &H70
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
      Debug.Print "Key F1 Pressed: " & CBool(GetAsyncKeyState(VK_F1))
    End Sub

  3. #3
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,945

    Re: Function Keys

    Instead of using KeyPress use the KeyDown event to capture function key code

    No need to use the API for this

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case 112
            MsgBox "You pressed F1"
        Case 113
            MsgBox "You pressed F2"
    End Select
    End Sub
    Last edited by DataMiser; Aug 17th, 2012 at 09:51 PM.

  4. #4
    Addicted Member
    Join Date
    Jul 11
    Posts
    214

    Re: Function Keys

    thank you so much

  5. #5
    Hyperactive Member
    Join Date
    Mar 08
    Posts
    500

    Re: [RESOLVED] Function Keys

    No need to use literals;
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyF1
            MsgBox "You pressed F1"
        Case vbKeyF2
            MsgBox "You pressed F2"
    End Select
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •