Multi Key Hotkeys in VB 2010 - vb.net

I've written a simple WAV player in VB 2010 - a Windows Form App with no menus. I'm trying to create a hotkey, like CTRL-D that would make a text box visible. Typing the correct password in the box will expand the form and show some Administrative functions. I'm having issues creating the hotkey with more than one key.
I've got the following code, but it does not work. I do have KeyPreview set to True.
Private Sub _KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Control And e.KeyCode = Keys.D Then
txtPWD.Visible = True
I've also tried
If e.Keycode = Keys.ControlKey & e.KeyCode = Keys.D
and many other variations including changing to ALT and SHIFT but nothing seems to work. If I remove one of the Keys, it works just fine. Where am I getting this wrong?

Check for Modifierkeys:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.modifierkeys.aspx

Related

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 :)

On key release handler

So, what I'm trying to do is run a piece of code when a button is pressed (I'll use the right-arrow key for the time being), and keep running it in a loop until that key is released.
So, my current code looks something like this (I've simplified it, because it's using SerialPorts, and is not easy to understand code):
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Right
Do Until (CODE LOOKING FOR RIGHT-ARROW KEY RELEASE)
SerialPort1.Write("right")
Loop
e.Handled = True
SerialPort1.Close()
End Sub
Ultimately, what I'm trying to do is control and arduino through the serial port function. I've wired it up to a cheap rc car (I usually have a few arduino projects on the go), and have code which allows me to control it by arrow keys in the IDE itself.
However, I have limited VB.NET knowledge (I've only been at it a few years), so I'm kinda struggling along with the help of Google, which was a god end while trying to work out how to keep the serialport open (it kept closing due to a logic error in my code).
The goal is that once I can control it by arrow keys in VB.NET, I can set up a more advanced program which will allow pre-programming routes (for example, I can try to "teach" the car sets of instructions so that I can get it to automatically follow a circuit, and load various pre-programmed routes into it). I'd also plan an on-screen gui to show what button was pressed, and whatever else I think of.
Also, given this is a prototype, I intend to eventually hook up the arduino and VB program to my hobby grade rc, and see if I can get the computer program, with pre-programmed isntructions, to beat my round my track.
Well, after that little essay, I guess it's just thanks in advance, and any advice in other areas of my project is also appreciated =)
Why make this complicated. It looks like you want to send something as long as the key is down, and then close the port when it is released.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Right AndAlso keydelay >= 1 Then
Debug.WriteLine("DWN") 'SerialPort1.Write("right")
e.Handled = True
ElseIf e.KeyCode = Keys.Right Then
keydelay += 1
End If
End Sub
Dim keydelay As Integer = 0
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Right Then
keydelay = 0
Debug.WriteLine("UP") 'SerialPort1.Close()
End If
End Sub

Multiple Keyboard Shortcuts

I am using the following code to try and get Ctrl+S to press a toolstrip button:
Private Sub take_register_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.S And Keys.Control Then
ToolStripButton20.PerformClick()
End If
End Sub
I am a newbie at this, so I dont understand millions of lines of coding, so can you please keep it as simple as possible :-) .
Total guesswork here since there is no actual question. First, in order to get something like that work, you need to set KeyPreview = True for the form. Next, you probably want to use the KeyDown event instead of KeyPress:
Private Sub Form1_KeyDown(...)
' when possible use AndAlso for speed and to avoid some errors in
' some situations. if e.Control is False, the second part wont be evaluated.
If e.Control AndAlso e.KeyCode = Keys.S Then
ToolStripButton20.PerformClick()
End If
End Sub
To repeat: you can simply assign a shortcut key combo to the menu object in the designer and let .NET do all the work. ...and I don't know where "multiple" comes in to play unless Ctrl+S counts as multiple somehow.

Making a VB.net winform button look pressed from code behind

I've got a basic VB.net 2.0 application together in VisualStudio 2005.
Within a form, I've tied [enter] in several text boxes to a button. What I'd like to do is "press" the button from getField_KeyDown() to give the user a visual indication of what's happening.
With the code below, the button click work is done but the button's look doesn't change and the user is left without feedback.
Private Sub getField_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtGetKey.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
Call btnGet.PerformClick()
End If
End Sub
How can I make the button looked pressed while the btnGet.PerformClick() work is being done?
You can make a checkbox and set the appearance to a button.
chkCheck.Appearance = Appearance.Button
You can also set this in the design mode.
And it behaves just the same as a normal checkbox. So you can assign a key pressed to the state of the checkbox.
You want to set the FlatStyle of the button. So your code will look like this:
Private Sub getField_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtGetKey.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
btnGet.FlatStyle = FlatStyle.Flat
End If
End Sub
This will change the button appearance - Don't forget to change it back to FlatStyle.Standard

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.