KeyPress Event in Visual Basic? - vb.net

I think this is possible, but I don't know... I want to check when the key A is pressed to move the player to the left, but for now just a messagebox.
Here is all the code I could find on the internet, it didn't work...
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.A Then
MsgBox("Left")
End If
End Sub
I am not asking how to do things when you enter stuff in a textbox, I'm asking how to run a event when you press a key.

Try this...
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 97 Or Asc(e.KeyChar) = 65 Then
MsgBox("hello")
End If
End Sub
This should work. 97 is 'a' and 65 is 'A' in ASCII.

Try this
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyCode = Keys.A Then
MsgBox("Left")
End Sub
or
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
Handles textBox1.KeyDown
If e.KeyCode = Keys.A Then
MsgBox("Left")
end if
End Sub

You probably want to set the form's KeyPreview property to true.
Doing this makes sure the form sees all key events even if one of
its child controls
Use the KeyEventArgs.KeyData property to see what key was pressed.
KeyCode , KeyData and KeyValue are members of
System.Windows.Forms.KeyEventArgs in the KeyUp and KeyDown events
only.

They actually simplified this from VB6.The new way with VB 2017 is...
Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProblem.KeyPress
If e.KeyCode = "a" or e.KeyCode = "A" Then
MsgBox("Left")
End If
End Sub

So, I got one step in the right direction maybe it will help. I'm trying to give control of the paddle to the player in a game of pong and found if textbox1 is highlighted or "has focus" then these commands will execute and I can move the paddle around the screen. The problem is there is also a textbox filling up with letters in the corner of the screen as this happens I'm not sure how to get rid of just yet. Perhaps it will be useful though.
Private Sub
Textbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 97 Then 'a
Paddle.Location = New Point(Paddle.Location.X - 10, Paddle.Location.Y)
End If
If Asc(e.KeyChar) = 119 Then 'w
Paddle.Location = New Point(Paddle.Location.X, Paddle.Location.Y - 10)
End If
If Asc(e.KeyChar) = 100 Then 'd
Paddle.Location = New Point(Paddle.Location.X + 10, Paddle.Location.Y)
End If
If Asc(e.KeyChar) = 115 Then 's
Paddle.Location = New Point(Paddle.Location.X, Paddle.Location.Y + 10)
End If
End Sub
Also, you can use this command to give focus to the textbox from a different event
Public Sub ControlSetFocus(control As Control)
If Control.CanFocus Then
control.Focus()
End If
End Sub

This works for me
If e.KeyChar = Convert.ToChar("a") Then
MsgBox(Convert.ToChar("a") +"enter key pressed ")
End If
Also you can use numbers and uppercase in ""

Related

e.Handled in RichTextBox has no effect. On TextBox, it works

The following code eats the Enter key for a TextBox (with "MultiLine" set to True):
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
End If
End Sub
The same code for a RichTextBox however doesn't work: The Enter key is not eaten:
Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
End If
End Sub
I don't see where I could have made a mistake.
Does anybody see where this behaviour might come from?
Got it.
I needs to be consumed in KeyDown. Unlike for TextBox:
Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
End If
End Sub

How to convert variable to double (KeyValue) in KeyDown in Visual Basic

I have this code:
Private Sub key(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyValue = Keys.Enter Then
If FuSt = False Then
FuSt = True
My.Computer.Audio.Play(My.Settings.Full)
Else
FuSt = False
My.Computer.Audio.Play(DIRECOTRY)
End If
End If
If e.KeyValue = b1 Then
My.Computer.Audio.Play(SoundsDir & "1.wav")
End If
End Sub
But when I want to play b1 (Z button on the keyboard) I have this error:
Conversation from "z" to Double is incorrect (Somethink like that)
Can you help me?
To assign keys to a variable b1 try as said below.
Keys is an enum.
Dim b1 As Keys
b1 = Keys.Z
Like  Hans Passant comment, if you want to use KeyPress event (recommended), try below code.
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Char.ToLower(ChrW(Keys.Z)) Then
MsgBox("Z selecetd")
End If
End Sub
or if you want to go with KeyDown event, try below code.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode.Equals(Keys.Z) Then
MsgBox("Z selecetd")
End If
End Sub

VB.NET - Check if the key pressed is the same as the text in label

I am wondering if anyone can help me to do the above question.
Basically what i have is a label which has had a randomly generated letter placed into it.
What I want to do is to show a msgbox (just to show that it works for now) when the same key in the label is pressed. I have tried two methods but none seem to work, could anyone point me in the right direction? It doesn't seem like it would be that hard, i am just new to coding.
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar = lblletter1.Text.ToString) Then
MsgBox("WORKS")
ElseIf (e.KeyChar = lblletter2.Text) Then
MsgBox("words")
End If
Many thanks!
In order to handle the form's KeyPress event, you need to set it's KeyPreview property. You can do that in the Form Designer or in the form's Load event handler as below.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = lblletter1.Text(0) Then
MessageBox.Show("WORKS")
ElseIf e.KeyChar = lblletter2.Text(0) Then
MessageBox.Show("words")
End If
End Sub
End Class
Try this
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar.ToString = lblletter1.Text) Then
MsgBox("WORKS")
ElseIf (e.KeyChar.ToString = lblletter2.Text) Then
MsgBox("words")
End If
End Sub
Try this
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar.Trim() = lblletter1.Text.Trim()) Then
MsgBox("WORKS")
ElseIf (e.KeyChar.Trim() = lblletter2.Text.Trim()) Then
MsgBox("words")
End If
End Sub

Howto check if control key is released

I am searching how to detect if control key, and no other keys, is released (in textbox).
This is what I came up so far:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = 17 Then
GetTick = System.Environment.TickCount
End If
End Sub
This work's but triggers also in combination keys like Ctrl+c etc...
With what to replace 17?
Have you tried e.Control, as in the following:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.Control Then
GetTick = System.Environment.TickCount
End If
End Sub
Adapted from:
http://www.devcurry.com/2009/10/detecting-ctrl-and-shift-keys-on-form.html
I haven't used VB in a while but here's some stuff that might help you.
You can try creating a boolean variable (true false), called ctrl or something like that. Make it true when the control key is first pressed, then make it false when they put the key up. Then you could create another variable that stores all the key codes called keycodes or something. Make keycodes -1 when a key other than control is pressed. Here is some example code :
Dim kys=-1
Dim ctrl=false
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
if e.KeyValue = Keys.Control then
ctrl=false
else
kys=-1
end if
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
if e.KeyValue = Keys.Control then
ctrl=true
else
kys=e.getKeyValue
end if
'Control c
if ctrl=true & kys=Keys.c then
MsgBox("It Worked!")
end if
End Sub
By the way, e.KeyValue is usually better than e.keyCode for stuff like this, it's easier to use because you can just use "Keys" instead of looking up all the keycodes.

Form keyPress help

first of all, I needed a way to know when the control key was down, and here's the link: Form keyDown not working?
Thanks to them I got it working. But I noticed that was not my ultimate objective!
Instead of checking for the control key on keyDown, I want to check for it on keyPress. But apparently I can't use
If e.Control Then
End If
On the
Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
End Sub
Method.
Any ideas? I also want to check for when the key is no longer pressed...
New problem:
Private Sub controlTick(ByVal sender As Object, ByVal e As EventArgs)
If Control.ModifierKeys = Keys.Control Then
controlActivated = True
PictureBox2.Invalidate()
End If
If Control.ModifierKeys <> Keys.Control Then
controlActivated = False
PictureBox2.Invalidate()
End If
Label1.Text = controlActivated
End Sub
That is inside a timer. For some reason it is always "False" unless I click somewhere with the control key pressed...
By the time you get the KeyPress event, which you won't when the form has any controls, the Control key state is already applied to the pressed key. So you'll get, say, Ctrl+V:
Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = ChrW(22) Then
MessageBox.Show("Ctrl+V pressed")
End If
End Sub
Ctrl+A = 1, etcetera, through Ctrl+Z = 26. This is very likely what you want to do, you could also use the Control.ModifierKeys property:
If Control.ModifierKeys = Keys.Control Then
MessageBox.Show("Control key pressed")
End If
Beware that many keys don't generate a KeyPressed event, like Ctrl+F1. KeyDown is required to test them.
You should use Control.ModifierKeys in whatever operation that should be affected by whether or not the control key is down. This timer's Tick event handler works fine:
Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Dim controlActivated As Boolean
If (Control.ModifierKeys And Keys.Control) = Keys.Control Then
controlActivated = True
End If
Label1.Text = controlActivated.ToString()
End Sub