System.Windows.Forms.KeyEventArgs

M

mp

is this namespace the only way to check

GetAsyncKeyState?

I'm doing something wrong as usual

i have:

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As
Integer

Private eKeyCode As KeyEventArgs

Public Function UserEscaped() As Boolean
eKeyCode = New KeyEventArgs(Keys.Escape)

If checkkey(CLng(eKeyCode.KeyCode)) Then
'If CBool(GetAsyncKeyState(CLng(eKeyCode.KeyCode.Space))) Then
MsgBox("Hit escape key")
Return True

End If
End Function


Function checkkey(ByVal lngKey As Long) As Boolean
If CBool(GetAsyncKeyState(lngKey)) Then
checkkey = True
Else
checkkey = False
End If
End Function



i'm not getting the msgbox when i hit the escape key

any tips?

thanks

mark
 
A

Armin Zingler

Am 20.07.2010 04:53, schrieb mp:
is this namespace the only way to check

GetAsyncKeyState?

I'm doing something wrong as usual

i have:

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As
Integer


Try it with a VB.Net declaration: ;-)

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short

or

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As System.Windows.Forms.Keys) As Short
Private eKeyCode As KeyEventArgs

Public Function UserEscaped() As Boolean
eKeyCode = New KeyEventArgs(Keys.Escape)

If checkkey(CLng(eKeyCode.KeyCode)) Then
'If CBool(GetAsyncKeyState(CLng(eKeyCode.KeyCode.Space))) Then
MsgBox("Hit escape key")
Return True

End If
End Function

Function checkkey(ByVal lngKey As Long) As Boolean


Function checkkey(ByVal Key As System.Windows.Forms.Keys) As Boolean


If CBool(GetAsyncKeyState(lngKey)) Then
checkkey = True
Else
checkkey = False
End If
End Function



i'm not getting the msgbox when i hit the escape key

any tips?

I don't want to butt in, but.... Have you noticed the meaning of
the return value of GetAsyncKeyState? You may want to check the most or
least significant bit of it:

http://msdn.microsoft.com/en-us/library/ms646293(VS.85).aspx

So my checkkey function was

Function checkkey(ByVal Key As System.Windows.Forms.Keys) As Boolean
return (GetAsyncKeyState(Key) and &H80000000)<>0
End Function


I wouldn't use a KeyEventArgs object (if not for a different
reason not shown here).
 
O

Onur Güzel

is this namespace the only way to check

GetAsyncKeyState?

I'm doing something wrong as usual

i have:

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As
Integer

Private eKeyCode As KeyEventArgs

Public Function UserEscaped() As Boolean
eKeyCode = New KeyEventArgs(Keys.Escape)

If checkkey(CLng(eKeyCode.KeyCode)) Then
'If CBool(GetAsyncKeyState(CLng(eKeyCode.KeyCode.Space))) Then
MsgBox("Hit escape key")
Return True

End If
End Function

Function checkkey(ByVal lngKey As Long) As Boolean
If CBool(GetAsyncKeyState(lngKey)) Then
checkkey = True
Else
checkkey = False
End If
End Function

i'm not getting the msgbox when i hit the escape key

any tips?

thanks

mark

You do not need p/invoke to get a keypress in windows.forms.form
class. You need to set KeyPreview property to true of the form. Then
handle KeyPress event:

Like:

Private Sub Form1_keypress _
(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles MyBase.KeyPress
If e.KeyChar = ChrW(Keys.Escape) Then
MsgBox("Hit Escape Key")
End If
End Sub

HTH,

Onur Guzel
 
A

Armin Zingler

Am 20.07.2010 12:58, schrieb Onur Güzel:
You do not need p/invoke to get a keypress in windows.forms.form
class. You need to set KeyPreview property to true of the form. Then
handle KeyPress event:

Like:

Private Sub Form1_keypress _
(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles MyBase.KeyPress
If e.KeyChar = ChrW(Keys.Escape) Then
MsgBox("Hit Escape Key")
End If
End Sub


This also fires if he presses Ctrl+[ (Ctrl+ü here on German keyboard)
not only if Esc is pressed. For capturing keys, the keydown event
fits better.
 
C

Cor

Armin,

I like more the keyup event (a key which is gone down, is almost forever
also gone up)

Cor

Armin Zingler said:
Am 20.07.2010 12:58, schrieb Onur Güzel:
You do not need p/invoke to get a keypress in windows.forms.form
class. You need to set KeyPreview property to true of the form. Then
handle KeyPress event:

Like:

Private Sub Form1_keypress _
(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles MyBase.KeyPress
If e.KeyChar = ChrW(Keys.Escape) Then
MsgBox("Hit Escape Key")
End If
End Sub


This also fires if he presses Ctrl+[ (Ctrl+ü here on German keyboard)
not only if Esc is pressed. For capturing keys, the keydown event
fits better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top