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 - 12693 Views - Last Post: 28 January 2010 - 04:36 PM Rate Topic: -----

#1 mak.frenz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-January 10

How to trap 'F1' key pressed or not?

Posted 27 January 2010 - 11:47 PM


'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



Is This A Good Question/Topic? 0
  • +

Replies To: How to trap 'F1' key pressed or not?

#2 chrixko  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 63
  • Joined: 25-November 08

Re: How to trap 'F1' key pressed or not?

Posted 28 January 2010 - 01:24 AM

Check if the KeyPreview-Property of the Form is set to True.

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

Was This Post Helpful? 0
  • +
  • -

#3 nmgod  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Re: How to trap 'F1' key pressed or not?

Posted 28 January 2010 - 05:56 AM

Alternatively you could use this, but the other ways are easier.

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

Was This Post Helpful? 0
  • +
  • -

#4 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: How to trap 'F1' key pressed or not?

Posted 28 January 2010 - 01:10 PM

nmgod, that's a global key hook which could be used to make a keylogger.
Was This Post Helpful? 0
  • +
  • -

#5 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: How to trap 'F1' key pressed or not?

Posted 28 January 2010 - 01:18 PM

View PostJack Eagles1, on 28 Jan, 2010 - 12:10 PM, said:

nmgod, that's a global key hook which could be used to make a keylogger.


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
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: How to trap 'F1' key pressed or not?

Posted 28 January 2010 - 01:18 PM

There is a a recent vb.net Snippet that would be helpful call KeyCombos.
Was This Post Helpful? 0
  • +
  • -

#7 T.Jackson  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 97
  • Joined: 27-January 10

Re: How to trap 'F1' key pressed or not?

Posted 28 January 2010 - 01:27 PM

View PostAdamSpeight2008, on 28 Jan, 2010 - 12:18 PM, said:

There is a a recent vb.net Snippet that would be helpful call KeyCombos.


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
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: How to trap 'F1' key pressed or not?

Posted 28 January 2010 - 02:07 PM

Picture Box Moving Example

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 

Was This Post Helpful? 0
  • +
  • -

#9 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: How to trap 'F1' key pressed or not?

Posted 28 January 2010 - 04:04 PM

I guess I'm just being pedantic, but it could theoretically be used to capture all key strokes... :D
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: How to trap 'F1' key pressed or not?

Posted 28 January 2010 - 04:36 PM

Who's mine?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1