Results 1 to 5 of 5

Thread: [RESOLVED] Check if F7 key is pressed

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Resolved [RESOLVED] Check if F7 key is pressed

    Is there are way to check if a key like F7 is pressed? I don't mean something like check to see when F7 is pressed then do something. F7 itself will do nothing when pressed. What I am looking for is when I click on a button on the form that it checks if F7 is pressed.

    I found the code "If My.Computer.Keyboard.ShiftKeyDown then" but this doesn't contain all buttons and I can't use the shift button. I also found other examples for textboxes with the e.KeyValue in the keypressed/keydown event but since the subroutine is triggered from a button I don't have the keypressed/keydown event. The key doesn't have to be F7 specifically. It can be something like Numpad + or numbers too. Just not the basic buttons like letters and numbers, alt, ctrl, shift, capslock, tab.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,478

    Re: Check if F7 key is pressed

    I would imagine that your best bet would be the GetKeyState Windows API function. There should be plenty of examples around, probably even on this very site. Just be sure that any examples you use are for VB.NET or, if they are for VB6, that you make the appropriate changes. The most common such change is using Integer rather than Long, but there may be others required.

  3. #3
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,587

    Re: Check if F7 key is pressed

    Code:
    Public Class Form1
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.F7 Then
                MsgBox("f7")
            End If
        End Sub
    End Class
    edit:

    Oops...this isn't what was asked for but I'll leave it in.
    Please remember next time...elections matter!

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,478

    Re: Check if F7 key is pressed

    Quote Originally Posted by jmcilhinney View Post
    I would imagine that your best bet would be the GetKeyState Windows API function.
    Actually, it occurs to me that it is usually the GetAsyncKeyState function that I've seen used. Not sure what the specific differences or whether you'd notice any difference in practice.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Check if F7 key is pressed

    jmcilhinney thank you for your answer. It indeed works as hoped even if I have no idea what it all means and does. And TysonLPrice also thank you for your time to try and help me.

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