[Help]Get a Key Press !?

Posts 1–11 of 11 · Page 1 of 1
[Help]Get a Key Press !?
Hi,

Here is my problem.
I want to get the key that the user press in my label1.text

Is that right ?

Code:
            If (GetAsyncKeyState(65)) Then
                Label1.Text = Label1.Text & "a"
            End If
And if it is do I put it in form1_load sub ?
Or like a While something True ?

Thanks for help guyz !
You do not use GetASyncKeyState for that.

Use the Form.KeyDown event.
Quote Originally Posted by freedompeace View Post
You do not use GetASyncKeyState for that.

Use the Form.KeyDown event.
What if he wants it to appear even when the form isn't focused, or when he has other controls focused on the form though? :/

I'll run through both methods though, you'd have to find a workaround for the Form_KeyDown if you have multiple controls as you can't select your actual form as if it were a control (i.e clicking on the blank form will not make it focused if there are other controls). Anywho:

[php]
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Label1.Text = e.KeyCode.ToString
End Sub
[/php]

And with GetAsyncKeyState (would require a timer!)

[php]
For i As Integer = 0 to 255
If GetAsyncKeyState(i) Then
Dim kCon As New KeysConverter
Label1.Text = kCon.ConvertToString(i)
TimerName.Stop
Exit For
End If
Next
[/php]
Quote Originally Posted by Jason View Post


What if he wants it to appear even when the form isn't focused, or when he has other controls focused on the form though? :/

I'll run through both methods though, you'd have to find a workaround for the Form_KeyDown if you have multiple controls as you can't select your actual form as if it were a control (i.e clicking on the blank form will not make it focused if there are other controls). Anywho:
It doesn't look like he wants to do that from what he just posted O:

And yes it will - use the System.Windows.Forms.Form.KeyPreview property.
Quote Originally Posted by freedompeace View Post
It doesn't look like he wants to do that from what he just posted O:

And yes it will - use the System.Windows.Forms.Form.KeyPreview property.
I'm not seeing your point with the KeyPreview...

Also, unless he has ONLY labels on his form (no buttons etc) handling only the form's keydown event won't work as expected (i.e not at all)

EDIT. After reading the documentation it seems KeyPreview is the way to go after all

[php]
Me.KeyPreview = True
[/php]

Put the above code right before you need to display the next keypress in a label,

Quote Originally Posted by Jason View Post


I'm not seeing your point with the KeyPreview...

Also, unless he has ONLY labels on his form (no buttons etc) handling only the form's keydown event won't work as expected (i.e not at all)

EDIT. After reading the documentation it seems KeyPreview is the way to go after all

[php]
Me.KeyPreview = True
[/php]

Put the above code right before you need to display the next keypress in a label,


Lol ! --"

At least you've cleared up one thing, but your last point... Just set it at InitialiseComponent(). Repeatedly calling it kills performance (by a lil bit).
Quote Originally Posted by freedompeace View Post
Lol ! --"

At least you've cleared up one thing, but your last point... Just set it at InitialiseComponent(). Repeatedly calling it kills performance (by a lil bit).
But if you don't want to send all keypresses to the main form to be handled?
Oh god... if he wants all KeyDown, KeyPress, KeyUp to be used, he uses KeyPreview propriety.. if not.. He will use the one that he feels like using. =D
The thing I want is :

Code:
If Key A Down Then
Label1.Text = Label1.Text & "a"
End if
But i'm not **** able to do this !!
Quote Originally Posted by Jason View Post


What if he wants it to appear even when the form isn't focused, or when he has other controls focused on the form though? :/

I'll run through both methods though, you'd have to find a workaround for the Form_KeyDown if you have multiple controls as you can't select your actual form as if it were a control (i.e clicking on the blank form will not make it focused if there are other controls). Anywho:

[php]
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Label1.Text = e.KeyCode.ToString
End Sub
[/php]

And with GetAsyncKeyState (would require a timer!)

[php]
For i As Integer = 0 to 255
If GetAsyncKeyState(i) Then
Dim kCon As New KeysConverter
Label1.Text = kCon.ConvertToString(i)
TimerName.Stop
Exit For
End If
Next
[/php]
Quote Originally Posted by killerld View Post
The thing I want is :

Code:
If Key A Down Then
Label1.Text = Label1.Text & "a"
End if
But i'm not **** able to do this !!

????????????????????
This work fine

Code:
    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        Select Case e.KeyCode
            Case Keys.A
                Label1.Text = "a"
        End Select

    End Sub
But when I replace TextBox1 by Form1 nothing work ... :S
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?