Howto check if control key is released - vb.net

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.

Related

keypress event instead click in vb.net

I'm trying to implement some events using keys instead of clicks, I know that this code:
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
My.Computer.Audio.Play("D:\VisualStudio\JuegoDIF\sonidos\sonidoPerro.wav")
End Sub
is playing the sound when i click the picturebox2, but how can you do this using a key ? example:
presing the key "f5" should play the same sound as the click event
so far i tried keyDown like this:
Private Sub PictureBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles PictureBox2.KeyDown
If e.KeyCode.Equals(Keys.F5) Then
My.Computer.Audio.Play("D:\VisualStudio\JuegoDIF\sonidos\sonidoPerro.wav") '
End If
End Sub
But it's not working, and the keypress im not sure how to implement it:
Private Sub PictureBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles
PictureBox2.KeyPress
End Sub
How can I do this implementation ?
Set the form properties to Key Preview to True
Under the Form KeyDown Event
Private Sub frm_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
Handles MyBase.KeyDown
If e.KeyCode = Keys.F1 Then
'Enter your Code here
End If
If e.KeyCode = Keys.F2 Then
'Enter Code here
End If
End Sub
This method should be the easiest one

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

Capture keys.TAB on KeyDown

I am trying to capture TAB keypress on Keydown Event.
I can see another post on How to fire an event when the tab key is pressed in a textbox?
However, On the above link, posted solution is not working for me which I mentioned below.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
'do something
End If
End Sub
For the testing purpose, I have added 2 simple textboxes on FORM1 and write the below code to capture the TAB on KeyDown event.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
MsgBox("TAB DOWN")
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Me.Text = e.KeyChar
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Tab Then
MsgBox("TAB UP")
End If
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Me.Text = "LEAVE"
End Sub
My above code should suppose to display a message box on KeyDown when TAB is press. It's not working.
Please let me know what I am doing wrong.
Thanks in advance!!!
I found a new event called PreviewKeyDown()
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
Me.Text = "TAB Capture From TextBox1_KeyDown At " & Now.ToString
End If
End Sub
Private Sub TextBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown
If e.KeyCode = Keys.Tab Then
Me.Text = "TAB Capture From TextBox1_PreviewKeyDown At " & Now.ToString
End If
End Sub
If you will execute the above code, you will able to capture TAB key on PreviewKeyDown() event.
MsgBox() is a holdover from VB6 and you should use the .NET implementation of a message box, like this:
MessageBox.Show("TAB UP")
Also, you are setting a Text property against the instance of the form class (Me), when I think you intend to set the Text property of the text box, like this:
Me.TextBox1.Text = e.KeyChar

VB.NET SetFocus Equivalent for Enter Button?

I have a textbox called clientFirst and, if I press Enter after I'm done typing in this textbox, I want the Enter button to represent a click on btnAdd. How can I make this happen?
Protected Sub btnAdd_Click(sender As Object, e As ImageClickEventArgs) Handles btnAdd.Click
'Do something
End Sub
There's not much code to post, really. :( But I was suggested to use the KeyDown function by Chase Ernst, which seems like a brilliant idea, except that it keeps telling me that a part of it is undefined. Here's the code I was suggested:
Private Sub clientFirst_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles clientFirst.KeyDown
If e.KeyCode = Keys.Enter Then
btnAdd.PerformClick()
End If
End Sub
It keeps telling me that System.Windows.Forms.KeyEventArgs is undefined. I'm guessing there's an Imports class I have to define at the top? Tried System.Windows, System.Object, System.Windows.Forms, System.KeyEventArgs and nothing works!
Thank you in advance! x
Easy way is to use the HTMLForm.DefaltButton property on the form.
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Page.Form.DefaultButton = "TextBox1"
End Sub
I would use something like this:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
Button1.PerformClick()
End If
End Sub
If the key that is pressed is the enter key, then Button1.PerformClick() will fire.

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