Visual Basic Key Listener - vb.net

I am trying to write a program in Visual Basic (with VS 2010) that will respond to the arrow keys. I am aware that there are key listener in Java but not sure if such thing exist in VB and how to code it. Please show me some example on this.
Thanks.

If you are doing winforms then set the KeyPreview property of the form to true and then set the KeyDown event. Your code will be like this:
Dim previousKey As Keys? = Nothing
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
'up arrow
End If
If e.KeyCode = Keys.Left Then
'left arrow
If Not previousKey Is Nothing And previousKey = Keys.Up Then
'Up arrow and then Left Arrow
MessageBox.Show("there, that's better")
End If
End If
If e.KeyCode = Keys.Right Then
'right arrow
End If
If e.KeyCode = Keys.Down Then
'down arrow
End If
'After everything is done set the current key as the previous key.
previousKey = e.KeyCode
End Sub

Related

How to Create Shift+F5 event in vb

I want to create a Shift+F5 key press event in VB.net. I used this code for creating Shift+F5 event in vb but it is not working.
Private Sub IBR_SJK_P110_000_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.Modifiers
Case Keys.Shift
If e.KeyCode = Keys.F5 Then
MsgBox("Shift F5 Pressed")
End If
End Select
End Sub
But if i used
If e.KeyCode = Keys.A Then
than It is working well.
Please Help me.
This is an example :
If e.KeyData = (Keys.F5 and Keys.Shift) Then
MessageBox.Show("got here")
End If
Another thing you can attempt is this:
If e.Shift = True AndAlso e.KeyCode = Keys.F5 Then
MsgBox("Shift F5 Pressed")
End If
The e.Modifiers property seems to be bugged on some computers/with some keyboards...

Can't get KeyDown event on Tabpage

I have a TabControl with several TabPages, each containing a series of PictureBox. I want to be able to navigate with the arrow keys inside those TabPages. I tried to give .Focus() to the TabPage when the user selects it, and then trap the PreviewKeyDown and KeyDown events, but it does not work. I've put a breakpoint at the PreviewKeyDown event handler, and it is not event reached.
Here is the activation of the TabPage :
Sub TilesetsTab_Selected(ByVal sender As Object, ByVal e As TabControlEventArgs) Handles TilesetsTab.Selected
Dim myTab As TabControl = CType(sender, TabControl)
If myTab.TabCount = 0 Then Exit Sub
Active.Tileset = Project.Tileset(CInt(myTab.SelectedTab.Tag))
Active.Tileset.Tab.Focus()
End Sub
And here is the Key event handlers
Sub Tab_KeyPreviewDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs) Handles Tab.PreviewKeyDown
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then e.IsInputKey = True
End Sub
Sub Tab_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Tab.KeyDown 'BUG
Dim TilesWidth As Integer = Me.Tab.Width \ Me.ScreenSize
Select Case e.KeyCode
Case Keys.Left
If Active.Tile.Index = 0 Then Exit Sub
Me.Tile(Active.Tile.Index - 1).Selected()
Case Keys.Right
If Active.Tile.Index = Project.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + 1).Selected()
Case Keys.Up
If Active.Tile.Index < TilesWidth Then Exit Sub
Me.Tile(Active.Tile.Index - TilesWidth).Selected()
Case Keys.Down
If Active.Tile.Index + TilesWidth > Active.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + TilesWidth).Selected()
End Select
End Sub
Why can't I fire a KeyDown event on the TabPage ?
I don't know why the answer posted yesterday by someone (and the comments I made) were deleted, but it was said that TabPages could not have focus, and that it is restricted to TabControl. Following that advice, I shifted the PreviewKeyDown and KeyDown event handlers to TabControl events and it worked.
I'm losing some of the arrow navigation between TabPages, but it suits me.

VB.net move between textboxes by keyboard arrow

I Have VB.net application form with 10 horizontal text boxes . I need to move between text boxes with right And Left Keyboard Arrow . Also I need Make textBoxes Format To Be Like This 0.00
In addition to webdad3's answer.
Private Sub Form1_KeyDown(ByVal sender as Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim tb as TextBox = TryCast(me.ActiveControl, TextBox)
If tb IsNot Nothing Then
Select Vase e.KeyCode
Vase Keys.Right, Keys.Left
dim forward as boolean = e.KeyCode = Keys.Right
me.SelectNextControl(Me, forward, True, True, true)
e.Handled = true
End Select
End If
End Sub
Don't forget to set Form.KeyPreview to true (via Forms Designer)
For the second part:
There are many, many different ways to format the text of a textbox.
The best solution would be to use DataBindings (complex topic, read a book about it.)
Public Class Form1
Public Property Price as Decimal
' call this code once
Private Sub InitControls()
Price = 3.45
me.txtPrice.DataBindings.Add(
"Text", Me, "Price", True,
DataSourceUpdateMode.OnPropertyChanged, Nothing, "0.00"
)
End Sub
End Class
I got the following code from the following link:
http://social.msdn.microsoft.com/Forums/windows/en-US/ffeeea42-f6ba-420f-827e-74879fd29b26/how-to-
detect-arrow-keys-in-vbnet?forum=winforms
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
' Sets Handled to true to prevent other controls from
' receiving the key if an arrow key was pressed
Dim bHandled As Boolean = False
Select Case e.KeyCode
Case Keys.Right
'do stuff
e.Handled = True
Case Keys.Left
'do other stuff
e.Handled = True
Case Keys.Up
'do more stuff
e.Handled = True
Case Keys.Down
'do more stuff
e.Handled = True
End Select
End Sub

Arrow keys don't seem to work?

I am trying to make a maze game however this is the only code I can't seem to get to work. I want the picture box (the player) to move in the direction of the arrow keys.
I have tried this code:
Private Sub BLevel1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = 37 Then
PictureBoxPlayer.Left = PictureBoxPlayer.Left - 10
ElseIf e.KeyCode = 38 Then
PictureBoxPlayer.Top = PictureBoxPlayer.Top - 10
ElseIf e.KeyCode = 39 Then
PictureBoxPlayer.Left = PictureBoxPlayer.Left + 10
ElseIf e.KeyCode = 40 Then
PictureBoxPlayer.Top = PictureBoxPlayer.Top + 10
End If
End Sub
I have also tried location codes but the arrow keys don't seem to move the picture box. This is the only issue I am running into.
Any ideas on how I can resolve this?
The cursor keys are special, they are used to navigate the focus from one control to another. So they are normally intercepted before they get to the control that currently has the focus. Furthermore, you wrote a KeyDown event for the form, it won't have the focus when the form has other controls so won't get the KeyDown event. It is unclear whether that applies here.
The best way to go about it is to intercept the cursor keys yourself, before they can be used elsewhere. You do so by writing an override for the form's ProcessCmdKey() method, like this:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Left Then
PictureBoxPlayer.Left = Math.Max(0, PictureBoxPlayer.Left - 10)
Return True
ElseIf keyData = Keys.Right Then
PictureBoxPlayer.Left = Math.Min(Me.ClientSize.Width - PictureBoxPlayer.Width, PictureBoxPlayer.Left + 10)
Return True
ElseIf keyData = Keys.Up Then
'' etc..
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I threw in the code to prevent the user from moving the player outside of the form. Finish the code by writing the Keys.Up and Keys.Down code.
Why don't you try to use Listbox1, make an image array and bring the listbox1 to back so it cant be seen, then addrange the listbox1 from the Folder Path of the image folder, It seems like simple image viewer.
You can try this
use the button or anything with click method
Dim myimg As Array
Private Sub img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles img.Click
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
myimg = IO.Directory.GetFiles(FolderBrowserDialog1.SelectedPath)
ListBox1.Items.AddRange(myimg)
ListBox1.SelectedIndex = 0
End If
End Sub
Then set the tab index to 1 on the listbox
then click twice the listbox1 and insert this
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.Refresh()
Me.BackgroundImage = Nothing
Me.BackgroundImage = Image.FromFile(ListBox1.SelectedItem.ToString)
End Sub
Use your arrow key, image will automatically change.
You can change Me to Picturebox.
Hope it helps

How do you detect simultaneous keypresses such as "Ctrl + T" in VB.NET?

I am trying to detect the keys "Control" and "t" being pressed simultaneously in VB.NET. The code I have so far is as follows:
Private Sub frmTimingP2P_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.ControlKey And e.KeyValue = Keys.T Then
MessageBox.Show("Ctrl + T")
End If
End Sub
I can detect one key or the other by removing the and statement and the second keyvalue statement, but I don't really get anything when I try this. Is there another method?
Thanks
First of all, And in your code should be AndAlso since it’s a logical operator. And in VB is a bit operator. Next, you can use the Modifiers property to test for modifier keys:
If (e.KeyCode And Not Keys.Modifiers) = Keys.T AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show("Ctrl + T")
End If
The e.KeyCode And Not Keys.Modifiers in the first part of the condition is necessary to mask out the modifier key.
If e.Modifiers = Keys.Ctrl can also be written as If e.Control.
Alternatively, we can collate these two queries by asking directly whether the combination Ctrl+T was pressed:
If e.KeyCode = (Keys.T Or Keys.Ctrl) Then …
In both snippets we make use of bit masks.
Private Sub frmMain_Zaporka_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyData
Case (Keys.Control + Keys.Shift + Keys.F12)
MsgBox("Control + Shift + F12")
Case (Keys.Escape)
Me.Close()
End Select
' or
If e.KeyCode = Keys.F12 AndAlso e.Modifiers = (Keys.Control Or Keys.Shift) Then
MsgBox("Control + Shift + F12")
ElseIf e.KeyCode = Keys.Escape Then
Me.Close()
End If
' or
Select Case e.KeyCode
Case (Keys.F12 And e.Control And e.Shift)
MsgBox("Control + Shift + F12")
Case (Keys.Escape)
Me.Close()
End Select
End Sub
I had the same problem, but for me to get this to work I had to set the forms KeyPreview property to true. In Visual studio you can change this in the Forms [Design] Property window or changing the property on load.
Private Sub frmTimingP2P_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
then use by using:
Private Sub frmTimingP2P_KeyDown(ByVal Sender As Object, ByVal e As _
System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If (e.KeyCode = Keys.T AndAlso e.Modifiers = Keys.Control) Then
MessageBox.Show("Ctrl + T")
End If
End Sub
or other program logic as provided in the answers above.
I'll save you from the long code.
Here:
If e.Control And e.Alt And e.KeyCode = Keys.G Then
MsgBox("Control Alt G")
End If
I dont have vb.net installed right now but try this on your keydown or keypress event:
If e.KeyCode = Keys.T AndAlso e.Control = True Then
MsgBox("Ctrl + T")
End If
I actually found through experimentation that the KeyPreview setting is irrelevant when code is processed through the "KeyDown" or "KeyUp" routines we add into our code. Perhaps the automatic inbuilt code for keypress takes the KeyPreview setting into account, but ours does not even have to consider it.
I found the best approach is the one shown in KuroMoro's answer, using e.KeyData plus various "Case Statements".
The following works beautifully to insert symbols into a textbox when certain keys are pressing with the Control key simultaneously.
Private Sub Comments_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Comment.KeyDown, Response.KeyDown
Select Case e.KeyData
Case (Keys.S + Keys.Control)
SendKeys.Send("♠")
Case (Keys.H + Keys.Control)
SendKeys.Send("♥")
Case (Keys.D + Keys.Control)
SendKeys.Send("♦")
Case (Keys.C + Keys.Control)
SendKeys.Send("♣")
End Select
End Subcode
Like Chris Raisin's approach, I use the KeyUp event. Otherwise, pressing the Control key can trigger an action before you press a second key. Here's an example of my code to test for CTRL-A in a listview control named lvSpectra:
Private Sub lvwSpectra_KeyUp(sender As Object, e As KeyEventArgs) Handles lvwSpectra.KeyUp
If e.Control And e.KeyCode = Keys.A Then
' Do something
End If
End Sub