Check for Control + Z - vb.net

How do I check if Control + Z is being pressed?
I managed to check for when one key is being pressed, but apparently I can't check for two at the same time.

Trap KeyDown event:
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If ((e.KeyCode = Keys.Z) AndAlso e.Control) Then
(do what you need)
End If
End Sub

Related

Specific keyboard characters to call specific button actions

When I start my program, and push the specified keys to call my button commands, it does not do anything.
The focus remains on one of the buttons and nothing occurs.
I've tried multiple codes from using KeyDown to KeyPress to codes that include vbKey.
I am very new to vb, so it is very likely that I just do understand what I am doing. :(
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = "/" Then
Call Button1_Click(sender, e)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close Program
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
'Add +1 to PMN Textbox (txtPMN)
txtPMN.Text = (Val(txtPMN.Text) + 1).ToString()
End Sub
End Class
I would like to simulate the clicking of certain buttons (activate the button code) when I press specific keys on the keyboard. For example: If I press "/" I would like Button1_Click to activate as if I clicked the button with the mouse. Then, if I push my next key ".", I would like Button2_Click to activate.
I do not want to use modifiers like: SHIFT, CTRL, ALT
Please try with keychar like this:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "/" Then
Call Button1_Click(sender, New EventArgs)
End If
end sub
when use keydown, the keycode for "/" key is OEM, depend the keyboard type, so char(e.keycode) may be not "/"

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

How to find out which key is pressed in keyDown event in visual basic?

I'm writing my first visual basic program, And I want to do something when for example the 'k' button is pressed, I know that I should write the code in "KeyDown" event, but I don't know how to find out that 'k' button is pressed or not
If you are using a Windows Forms Application, you have to put the KeyPreview property of the form to True so the form will monitorize key events.
Then:
Private Sub Form1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "k" OrElse e.KeyChar = "K" Then
MessageBox.Show("Pressed!")
End If
End Sub
If you prefer, you could use other event:
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.K Then
MessageBox.Show("Pressed!")
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