'I want to call a function or show a Help formwhen user presses 'F1' key. for e.g. show application Help to user after
'pressing F1 key on main form. I dont know how to trap whether user pressed F1 key or not
Private Sub frmMain_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Dim KeyAscii As Long = Asc(e.KeyChar)
If (KeyAscii = 27) Then
'MsgBox("Key pressed")
objHelp.FrmShowHelp()
End If
End Sub
How to trap 'F1' key pressed or not?I want to call a function or show a form when user presses 'F1'
Page 1 of 1
9 Replies - 9677 Views - Last Post: 28 January 2010 - 04:36 PM
#1
How to trap 'F1' key pressed or not?
Posted 27 January 2010 - 11:47 PM
Replies To: How to trap 'F1' key pressed or not?
#2
Re: How to trap 'F1' key pressed or not?
Posted 28 January 2010 - 01:24 AM
Also it's easier to use the KeyDownEvent:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F1 Then
MsgBox("Key pressed")
End If
End Sub
This post has been edited by chrixko: 28 January 2010 - 01:28 AM
#3
Re: How to trap 'F1' key pressed or not?
Posted 28 January 2010 - 05:56 AM
Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal key As Integer) As Integer
This post has been edited by nmgod: 28 January 2010 - 05:56 AM
#4
Re: How to trap 'F1' key pressed or not?
Posted 28 January 2010 - 01:10 PM
#5
Re: How to trap 'F1' key pressed or not?
Posted 28 January 2010 - 01:18 PM
Jack Eagles1, on 28 Jan, 2010 - 12:10 PM, said:
Great API, I use it all the time for making games.
I wouldn't call it a hook though -- it's not sub-classing, just a simple API.
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short do ' Fetch user input Call ScanKeyboard() loop Private Sub ScanKeyboard() '------------------------------------------------------------------------------------------------------------------ ' Purpose: Fetch keyboard strokes from user ... '------------------------------------------------------------------------------------------------------------------ If GetAsyncKeyState(Keys.Right) Then MoveRight = True ElseIf GetAsyncKeyState(Keys.Left) Then MoveLeft = True ElseIf GetAsyncKeyState(Keys.Up) Then MoveUP = True ElseIf GetAsyncKeyState(Keys.Down) Then MoveDown = True ElseIf GetAsyncKeyState(Keys.F2) Then If Not DoIntro Then If Not ShowGameOver Then NewGame = True GameOver = True End If End If End If AppRunning = GetAsyncKeyState(Keys.Escape) End Sub
easy greasy,
Trent Jackson
#7
Re: How to trap 'F1' key pressed or not?
Posted 28 January 2010 - 01:27 PM
AdamSpeight2008, on 28 Jan, 2010 - 12:18 PM, said:
The problem with these keydown like events is that when the object loses focus (and you can't always predict what the user may do either or constantly force refocus) -- they cease to work.
You just can not properly make games using them.
Trent Jackson
#8
Re: How to trap 'F1' key pressed or not?
Posted 28 January 2010 - 02:07 PM
Public Class Form1 Dim k As KeyCombos Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load k = New KeyCombos k.AddCombo(AddressOf MovePlayLeft, Keys.Left) k.AddCombo(AddressOf MovePlayRight, Keys.Right) k.AddCombo(AddressOf MovePlayUp, Keys.Up) k.AddCombo(AddressOf MovePlayDown, Keys.Down) End Sub Public Sub MovePlayLeft() If Me.PictureBox1.Left > 0 Then Me.PictureBox1.Left -= 4 End Sub Public Sub MovePlayRight() If Me.PictureBox1.Left < Me.Width Then Me.PictureBox1.Left += 4 End Sub Public Sub MovePlayUp() If Me.PictureBox1.Top > 0 Then Me.PictureBox1.Top -= 4 End Sub Public Sub MovePlayDown() If Me.PictureBox1.Top < Me.Height Then Me.PictureBox1.Top += 4 End Sub Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown k.KeyDown(e.KeyCode) End Sub Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp k.KeyUP(e.KeyCode) End Sub End Class
#9
Re: How to trap 'F1' key pressed or not?
Posted 28 January 2010 - 04:04 PM
#10
Re: How to trap 'F1' key pressed or not?
Posted 28 January 2010 - 04:36 PM
|
|

New Topic/Question
Reply




MultiQuote





|