How can I Move objects diagonally when two directional keys are pressed? How to do that?
I tried adding elseif statement for Handling Up and Right together but It still move up or right when i press them together
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Right Then
Label1.Location = New Point(Label1.Location.X + 5, Label1.Location.Y)
ElseIf e.KeyCode = Keys.Left Then
Label1.Location = New Point(Label1.Location.X - 5, Label1.Location.Y)
ElseIf e.KeyCode = Keys.Down Then
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y + 5)
ElseIf e.KeyCode = Keys.Up Then
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y - 5)
ElseIf e.KeyCode = Keys.Up And e.KeyCode = Keys.Right Then
Label1.Location = New Point(Label1.Location.X + 5, Label1.Location.Y + 5)
End If
CheckIntersections()
If Label1.Location.Y < 0 Then
Label1.Location = New Point(Label1.Location.X, Me.Height)
ElseIf Label1.Location.Y > Me.Height Then
Label1.Location = New Point(Label1.Location.X, Me.Bottom = 0)
ElseIf Label1.Location.X < 0 Then
Label1.Location = New Point(Me.Width, Label1.Location.Y)
ElseIf Label1.Location.X > Me.Width Then
Label1.Location = New Point(0, Label1.Location.Y)
End If
End Sub
The KeyDown event occurs each time a single key is pressed. Therefore you'll need to remember when a cursor key is pressed on once occurrence of KeyDown to check in another occurrence for another cursor key being pressed.
Remember to subscribe to KeyUp events to clear the state because the use could press one cursor key, release it and then press another.
Your code will never work:
ElseIf e.KeyCode = Keys.Up And e.KeyCode = Keys.Right Then
because this cannot be true. KeyCode is a single key code, it cannot be both one key and another.
Related
I am trying to make a game in Win forms using VB from scratch which i know is a bad idea but i like the challenge. Therefore i have been testing movement systems so i can choose my favourite , however i have ran into an issue as trying to move with a picture box as when i press the movement key the picture box just starts erasing the image in the direction i wanted to move until it disappears.
I am using
Public Class Form1
Dim RightM As Boolean
Dim LeftM As Boolean
Dim UpM As Boolean
Dim DownM As Boolean
Sub Movement()
Do While UpM = True
PictureBox1.Top += -5
Threading.Thread.Sleep(20)
Loop
Do While LeftM = True
PictureBox1.Left += -5
Threading.Thread.Sleep(20)
Loop
Do While DownM = True
PictureBox1.Top += 5
Threading.Thread.Sleep(20)
Loop
Do While RightM = True
PictureBox1.Left += 5
Threading.Thread.Sleep(20)
Loop
Do While (UpM = True) And (RightM = True)
PictureBox1.Top += -5
PictureBox1.Left += 5
Threading.Thread.Sleep(20)
Loop
Do While (UpM = True) And (LeftM = True)
PictureBox1.Top += -5
PictureBox1.Left += -5
Threading.Thread.Sleep(20)
Loop
Do While (DownM = True) And (RightM = True)
PictureBox1.Top += 5
PictureBox1.Left += 5
Threading.Thread.Sleep(20)
Loop
Do While (DownM = True) And (LeftM = True)
PictureBox1.Top += 5
PictureBox1.Left += -5
Loop
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.A Then
LeftM = True
Movement()
ElseIf e.KeyCode = Keys.D Then
RightM = True
Movement()
ElseIf e.KeyCode = Keys.W Then
UpM = True
Movement()
ElseIf e.KeyCode = Keys.S Then
DownM = True
Movement()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.A Then
LeftM = False
Movement()
ElseIf e.KeyCode = Keys.D Then
RightM = False
Movement()
ElseIf e.KeyCode = Keys.W Then
UpM = False
Movement()
ElseIf e.KeyCode = Keys.S Then
DownM = False
Movement()
End If
End Sub
End Class
The question took me back to the Z80 days, I got a little nostalgic so decided to post a simplified answer.
'Position of PictureBox
Private Plocation As Point
'PictureBox Movement Boundries
Private XL As Integer 'X - Left
Private XR As Integer 'X - Right
Private YT As Integer 'Y - Top
Private YB As Integer 'Y - Bottom
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'Set Boundries
XL = 0
XR = Me.ClientSize.Width - PictureBox1.Width
YT = 0
YB = Me.ClientSize.Height - PictureBox1.Height
'Position PictureBox roughly in the centre of the Form
Plocation = New Point((Me.ClientSize.Width / 2) - (PictureBox1.Width / 2), (Me.ClientSize.Height / 2) - (PictureBox1.Height / 2))
PictureBox1.Location = Plocation
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
'Change X,Y depending on KeyPress
If e.KeyCode = Keys.A Then Plocation.X -= 5
If e.KeyCode = Keys.D Then Plocation.X += 5
If e.KeyCode = Keys.W Then Plocation.Y -= 5
If e.KeyCode = Keys.S Then Plocation.Y += 5
'Check for X,Y boundries
If Plocation.X < XL Then Plocation.X = XL
If Plocation.X > XR Then Plocation.X = XR
If Plocation.Y < YT Then Plocation.Y = YT
If Plocation.Y > YB Then Plocation.Y = YB
'Update Position
PictureBox1.Location = Plocation
End Sub
I'm intentionally checking all key presses one after another, helps with diagonal movements. It's a little choppy and basic but may give you some ideas. Happy programming...
I am trying to capture a KeyUp event when the mouse is over the map. I am using GMaps library.
I have tried using the keypress, keydown and keyup events of my gmap control (where the map is placed), and I have tried to use the keypress, keydown and keyup from the form events, but nothing happens.
In my form, I have also some buttons, and what I see when I press one of the rows keys is that the focus is changing among the buttons, The effect is as if you wanted to select a button using the arrows on the keyboard, and I don't know why is doing that, if I have a specific code in the key events of the program.
In the MouveHover event I am using this code:
Private Sub GMapControl_MouseHover(sender As Object, e As EventArgs) Handles GMapControl.MouseHover
If Keys.Up <> Keys.None Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat + 1, GMapControl.Position.Lng)
End If
End Sub
But I have not the behavior I am looking for. Also I am trying to use the KeyUp event of the form:
Private Sub BMS_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode.ToString = "Up" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat + 1, GMapControl.Position.Lng)
ElseIf e.KeyCode.ToString = "Down" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat - 1, GMapControl.Position.Lng)
ElseIf e.KeyCode.ToString = "Left" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng - 1)
ElseIf e.KeyCode.ToString = "Right" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng + 1)
End If
End Sub
Also I have tried the same in the KeyUp event of the GMap control:
Private Sub GMapControl_KeyUp(sender As Object, e As KeyEventArgs) Handles GMapControl.KeyUp
If e.KeyCode.ToString = "Up" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat + 1, GMapControl.Position.Lng)
ElseIf e.KeyCode.ToString = "Down" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat - 1, GMapControl.Position.Lng)
ElseIf e.KeyCode.ToString = "Left" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng - 1)
ElseIf e.KeyCode.ToString = "Right" Then
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng + 1)
End If
End Sub
And nothing...
Maybe someone has some ideas to do this?
Thanks!
Sorry, I have continued finding an answer and I have found the response of what I was looking for on this thread
https://social.msdn.microsoft.com/Forums/windows/en-US/ffeeea42-f6ba-420f-827e-74879fd29b26/how-to-detect-arrow-keys-in-vbnet?forum=winforms
By using the property:
Me.KeyPreview = True
In the Load method of the form, and using this code in the KeyUp of the form:
Private Sub BMS_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Right
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng + 0.1)
e.Handled = True
Case Keys.Left
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat, GMapControl.Position.Lng - 0.1)
e.Handled = True
Case Keys.Up
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat + 0.1, GMapControl.Position.Lng)
e.Handled = True
Case Keys.Down
GMapControl.Position = New PointLatLng(GMapControl.Position.Lat - 0.1, GMapControl.Position.Lng)
e.Handled = True
End Select
End Sub
Anyway I would like to ask one thing I don't understand. I have tried this before but without using the Me.KeyPreview property, and without using the e.Handled = true property.
Could someone explain to me why using this two lines I have the behavior I want, and why without using them I was not able to do it?
Thanks!
I have a Datagridview on my form with two columns. I only want the cells in the first column to be selected. So I'm using the datagridview's keydown event to capture the TAB and SHIFT+TAB. So if the first row is selected and the user presses tab, the second row will be selected instead of staying on the first row and selecting the second column's cell.
My if statement for the TAB key is working fine, but for some reason the SHIFT+TAB statement isn't. The cell above the currently selected cell isn't getting selected. Nothing happens.
Private Sub myGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myGrid.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
SendKeys.Send("{DOWN}")
End If
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
SendKeys.Send("{UP}")
End If
End Sub
The really weird thing though is if I add a messagebox into the statement it works.
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
MsgBox("test")
e.SuppressKeyPress = True
SendKeys.Send("{UP}")
End If
Any ideas?
Sendkeys is a last, last resort...
How about something like this:
Private Sub dgv_KeyDown(sender As Object, e As KeyEventArgs) Handles dgv.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
dgv.CurrentCell = dgv(0, dgv.CurrentCell.RowIndex + 1)
End If
End Sub
With the help from #rheitzman I was able to see the main issue. The issue came from having the TAB and SHIFT+TAB in two different if statements. Here's my updated code:
Private Sub myGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myGrid.KeyDown
Dim row As String = Me.myGrid.CurrentCellAddress.Y
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
Me.myGrid.CurrentCell = Me.myGrid.Rows(row - 1).Cells(0)
Me.myGrid.Rows(row - 1).Cells(0).Selected = True
ElseIf e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
Me.myGrid.CurrentCell = Me.myGrid.Rows(row + 1).Cells(0)
Me.myGrid.Rows(row + 1).Cells(0).Selected = True
End If
End Sub
Tab and Shift+Tab are already handled by Windows Forms and DataGridView. They move the cell highlighter/selector move left(previous) and right(next). You'd have to create a class that inherits DataGridView and manually override their functionality to achieve what you want, then use that new class for your data grid.
Private Sub DataGridView1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
Dim r As Integer = Me.DataGridView1.CurrentCellAddress.Y
Dim f As Integer = Me.DataGridView1.CurrentCellAddress.X
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
If 0 = r Then
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(f - 1)
Me.DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(f - 1).Selected = True
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(r - 1).Cells(f)
Me.DataGridView1.Rows(r - 1).Cells(f).Selected = True
End If
ElseIf e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
If DataGridView1.RowCount = (r + 1) Then
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(0).Cells(f + 1)
Me.DataGridView1.Rows(0).Cells(f + 1).Selected = True
Else
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(r + 1).Cells(f)
Me.DataGridView1.Rows(r + 1).Cells(f).Selected = True
End If
End If
End Sub
I have a problem when I try to make Keyboard Tester application for my small office.
I cant detect print screen keycode e.keycode = keys.PrintScreen.
I will do something like change picturebox back color when a key down, but it seems doesnt work with print screen, nothing happen.
my code is:
Private Sub keyboardmenu_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
'Esc + Function Keys -----------------------------------------
If e.KeyCode = Keys.Escape Then
EscBox.BackColor = Color.Red
End If
If e.KeyCode = Keys.F1 Then
F1Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F2 Then
F2Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F3 Then
F3Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F4 Then
F4Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F5 Then
F5Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F6 Then
F6Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F7 Then
F7Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F8 Then
F8Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F9 Then
F9Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F10 Then
F10Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F11 Then
F11Box.BackColor = Color.Red
End If
If e.KeyCode = Keys.F12 Then
F12Box.BackColor = Color.Red
End If
'End of Esc + Function Keys -----------------------------------------
Private Sub keyboardmenu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
'Esc + Function Keys ----------------------------------------
If e.KeyCode = Keys.F1 Then
F1Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F2 Then
F2Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F3 Then
F3Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F4 Then
F4Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F5 Then
F5Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F6 Then
F6Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F7 Then
F7Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F8 Then
F8Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F9 Then
F9Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F10 Then
F10Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F11 Then
F11Box.BackColor = Color.Transparent
End If
If e.KeyCode = Keys.F12 Then
F12Box.BackColor = Color.Transparent
End If
'End of Esc + Function Keys -----------------------------------------
End Sub
Please help me. Idk if there are more keys like print screen problem.
Thank You
I'm not sure of the real reason, but I've read about it before and came to the conclusion that Windows is protecting that key's event from being easily handled. Someone else probably knows better, but this works:
Protected Overrides Function ProcessKeyEventArgs(ByRef msg As Message) As Boolean
If msg.WParam = Keys.PrintScreen Then
MessageBox.Show("PrintScreen key press detected!")
End If
Return MyBase.ProcessKeyEventArgs(msg)
End Function
Also, you should put all of those if statements in a Select Case statement:
Private Sub keyboardmenu_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Escape
EscBox.BackColor = Color.Red
Case Keys.F1
F1Box.BackColor = Color.Red
Case Keys.F2
F2Box.BackColor = Color.Red
'Etc
End Select
End Sub
This comment is on the IF statements and CASE suggestion, otherwise I concur with the answer by Keith.
I use this sort of thing fairly often, but it's crazy to put all those IF statements, OR the Case statement:
Simply name the boxes to match the enumeration, then (assuming Controls is the parent container of all the boxes)
Controls(e.keycode.tostring & "box").backcolor = Color.Red
and
Controls(e.keycode.tostring & "box").backcolor = Color.Transparent
This one line will replace everything (if you rename the escbox to escapebox)
Of course, you might want to do some checking like
If Controls.ContainsKey(e.keycode.tostring & "box") Then ...
Can you help me with this in VB.Net
Im trying to have a shortcut here for my AnnotateViewer
when i press "Ctrl + Down"
the view must scroll down.
Here's the code that i tried
Dim pt As Point = iView.ScrollPosition
If e.Control And e.KeyCode = Keys.NumPad0 Then
pt.Y -= 99999
Msg("Scroll Down")
End If
iView.ScrollPosition = new Point(x,y)
Change the value of x for horizontal scroll, and y for vertical scroll bar.
yeah now it works.
I used these codes
If e.Control And e.KeyCode = Keys.NumPad2 Then
iView.ScrollPosition += New Point(0, -90)
ElseIf e.Control And e.KeyCode = Keys.NumPad8 Then
iView.ScrollPosition += New Point(0, 90)
ElseIf e.Control And e.KeyCode = Keys.NumPad4 Then
iView.ScrollPosition += New Point(50, 0)
ElseIf e.Control And e.KeyCode = Keys.NumPad6 Then
iView.ScrollPosition += New Point(-50, 0)
End If