How to check the key pressed in VB - vb.net

I am trying to make a program which needs you to select a hotkey, but you can only give a character from the code I've produced, where as I want to be able to set something like F9 as well. I have been through a few on this site and this is the closest to what I want. I'm using Visual Studio 2013.
This is what I've got so far.
Private Sub Configure_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
MsgBox("Your new hotkey is: " & e.KeyChar)
End Sub
Thanks in advance.

The KeyPress event is not raised by non-character keys other than space and backspace; however, the non-character keys do raise the KeyDown and KeyUp events.
Set ReadOnly property of your TextBox1 to true and handle keyDown event:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
Me.TextBox1.Text = e.KeyData.ToString()
End Sub
This shows also the key combinations in te text box, for example if you press Ctrl + Shift + F9 you will see F9, Shift, Control in text box.
You can use KeyCode, Modifiers and other properties of KeyEventArgs to gain information about keys.

Related

Get Key Pressed in Variable - Visual Studio Basic 2017

I am trying to make a simple program, pasting in whatever you write in a textbox. I am doing this in Visual Studio 2017. It is a Windows-Forms App with Visual Basic. It works in the current state, but I woud like to add the ability to customize the key. Right now, when you press F12, it sends the message. I want a simple way of asking the user for a key, and after he presses it, it gets set as the new paste key. Any idea how I can accomplish this? Thanks in advance.
My code below, where I have Keys.F12 I have/want a variable that holds the key set by the user.
Dim hotkey As Boolean
hotkey = GetAsyncKeyState(Keys.F12)
If hotkey = True Then
SendKeys.Send(TextBox1.Text)
End If
I guess this is what you're looking for:
Private myNewKey As Char
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Convert.ToChar(myNewKey) Then
e.Handled = True ' to prevent getting the Press Key to be entered
TextBox1.Text = Clipboard.GetText
End If
End Sub
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
myNewKey = e.KeyChar
Label1.Text = "New paste key set to: " + e.KeyChar.ToString
End Sub
This program changes the pasting key each time user changes it by their own wish. Just simply declare a variable which holds the new specified key and when the user will get asked to enter a new press key, the variable should be assigned to it.
Eventually, when the user hits the same key, the TextBox1 KeyPress event will be triggered and the TextBox's text will be set by Clipboard.GetText.
Key set to 5 and when user hits that during focus on TextBox1 (second box), the text will get pasted.
If e.KeyCode = Keys.F12 Then
SendKeys.Send(TextBox1.Text)
End If

I want to show a form by pressing the f5 key but it doesn't work in vb.net

hello I'm just writing a program and I need to show a new form when I'm on a text_box and press the f5 key
but its seem that the program doesn't recognize the f5 key, because I eve did a mini-program to find out the keychar of the f5 key and didn't work
I don't know if someone could help me with this
this is my code:
Private Sub txt_numero_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt_numero.KeyPress
If e.KeyChar = Convert.ToChar(Keys.F5) Then
fmrBuscarByName.Show()
End If
You may use the KeyUp event too!
... txt_numero.KeyUp
If e.KeyCode = Keys.F5 Then
Form1.ShowDialog()
End If
End Sub
Handle KeyDown rather than KeyPress:
If e.KeyCode = Keys.F5 Then

How do i detect Alt + Tab Key and then close the form?

I want to close my form when Alt + Tab is pressed. However, The form is somehow not registering the key combination.
i have tried to use the Me.KeyUp event to detect the keys
Private Sub Menu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyData
Case (Keys.Alt + Keys.Tab)
Close()
End Select
End Sub
How can it be done ?
Try this: As in my comment Alt+Tab would not work. So try something else like Alt+Q. Put this code under the Form's keydown event.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If (e.KeyCode = Keys.Q AndAlso e.Modifiers = Keys.Alt) Then
Me.Close()
End If
End Sub
You need to set the form's Set KeyPreview to True for it to respond to the key events, but since Alt + Tab is a special windows combination the key events will not be fired.
Try in this way
Dim myval As Integer 'this is global variable declare
Private Sub Menu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyData
Case (Keys.Alt)
myval = 1
Case (Keys.Tab)
If myval = 1 Then
Me.Close()
End If
End Select
End Sub
May be my syntax in not correct, please edit it if anything is notok.
I managed to work around this problem using this method-
That's usually the ESC key. One key press. A common one. Maybe you want to handle the Form's deactivate event, instead. – Jimi
I used the forms deactivate event to close it, since Alt+Tab is basically deactivating the form!
(A workaround because Alt+Tab as keystrokes were not detected by any means, as mentioned by
the last two answers.)
My final code looks like this -
Private Sub Menu_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
If loaded = true Then
Close()
End If
End Sub
I had to check if loaded (a boolean i declared to be set to true when everything is loaded)
to be true because my form opens one other form while loading, which inadvertently deactivated the main form and closed it!
Thank you so much for helping me everyone!!
Edit: I needed to clarify that the second form is always behind my main form so it is never really clicked on. I did this as a workaround to have aero blur in my application without a bug i experienced while applying it to the main form - it is too light, so text is unreadable. So i set another form to show aero blur and always stay behind my main form, whose opacity I have set to 0.88. This gives me a lot of control over the look of the blur.

VB 2008 net keycode

Can anyone help on how to disable the alt + ctrlkey in vb.net. I am not using any button to disable. But by default it should start function disable to keys once the application starts.
You can use the SuppressKeyPress Property. I'm not sure what you are trying to do, but lets say that you dont want a certain key to effect a textbox change. Lets say you don't want the key G to be written in the text box. You can write to following code:
Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.G Then
e.SuppressKeyPress = True
End If
End Sub
This code will prevent the key G to be written in the text box. Hoped it helped :)

VB.NET 2008 - How Do I Make Button.click Event Perform both the enter and period(del) keys on the numerical keyboard?

How Do I Make Button.click Event Perform both the enter and period(del) keys on the numeric keypad?
Code snippet:
Private Sub frmCalc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad0
btn0.PerformClick()
Case Keys.NumPad1
btn1.PerformClick()
'etc.
End Select
End Sub
Case Keys.Enter and Case Keys.Separator do not work. Nor does anything like Keys.OEMPeriod for the period(del) key.
I've also tried calling the enter key in a KeyPress event etc. But to no avail.
Any ideas? I'm trying to mimic Windows calc.exe for a school project and thought I'd try throwing in a few extras such as numeric keypad functionality.
You can figure this out by yourself.
Set a breakpoint at the beginning of your KeyDown handler in the debugger, press one of those keys, and check the value of e.KeyCode in the Locals window.