Results 1 to 6 of 6

Thread: Capture ALT+X

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Capture ALT+X

    Hey I need to have an Exit button including keyboard shortcut Alt+X to go to Form 2. But this doesnt work. Any help?

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If (Chr(13) + Chr(88)) Then
                Me.Hide()
                Dim fr2 As New Form2
                fr2.Show()
    
            End If
        End Sub
    Last edited by angelica; Dec 6th, 2008 at 06:35 PM.
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Capture ALT+X

    try this:

    vb Code:
    1. Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Integer) As Integer
    2. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    3.     If GetAsyncKeyState(17) And e.KeyCode = Keys.X Or GetAsyncKeyState(18) And e.KeyCode = Keys.X Then
    4.        MsgBox("ALT+X pressed")
    5.     End If
    6. End Sub

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Capture ALT+X

    forgot to mention, you need to set your form .keypreview property to true

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Capture ALT+X

    actually i just found a better way without the api function:

    vb Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.     If e.Modifiers = Keys.Alt And e.KeyCode = Keys.X Then
    3.        MsgBox("ALT+X pressed")
    4.     End If
    5. End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: Capture ALT+X

    Hi .paul,

    Works fine. Can you please explain to me this bit and what is actually triggers?

    Code:
    Public Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Integer) As Integer
    thanks

    Is there a simpler way of doing it because this was an exercise for a very very beginner's exercise to young students!
    Last edited by angelica; Dec 6th, 2008 at 07:15 PM.
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Capture ALT+X

    sorry, i made a mistake earlier. i wasn't concentrating.
    if you have a button with the text "Exit", change it to "E&xit", then alt+x is your buttons shortcut.

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