Detect Print Screen keyup and keydown in Keyboard Tester app VB NET - vb.net

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 ...

Related

2D Collision detection in Visual Basic

This is my first time writing a small winform game. When researching into collision detection i know that i need to set my bounds first.
Me.Controls.Add(PlayerShip) 'imports the picture box onto the PlayScreen
PlayerShip.Width = 40
PlayerShip.Height = 40 'Dimensions of the player ship
PlayerShip.BorderStyle = BorderStyle.FixedSingle 'adds a border to the picturebox
PlayerShip.BackColor = Color.White 'adds colour to the ship background
PlayerShip.SetBounds(x:=40, y:=40, height:=40, width:=40)
And then the other picture box
Me.Controls.Add(WallNorth)
WallNorth.Width = 750
WallNorth.Height = 5
WallNorth.BorderStyle = BorderStyle.FixedSingle
WallNorth.BackColor = Color.Green
WallNorth.Top = 1
WallNorth.Left = 1
WallNorth.SetBounds(x:=1, y:=1, height:=5, width:=750)
Private Sub PlayScreen_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyValue
Case Keys.Right
MoveRight = True
Case Keys.Left
MoveLeft = True 'This edits the boolean value of the varibales when the correct key is pressed
Case Keys.Up
MoveUp = True
Case Keys.Down
MoveDown = True
End Select
End Sub
Private Sub PlayScreen_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyValue
Case Keys.Right
MoveRight = False
Case Keys.Left
MoveLeft = False 'This edits the boolean value when the key has been lifted
Case Keys.Up
MoveUp = False
Case Keys.Down
MoveDown = False
End Select
End Sub
Private Sub MovementTimer_Tick(sender As Object, e As EventArgs) Handles MovementTimer.Tick
If MoveRight = True Then
PlayerShip.Left += 5
End If
If MoveLeft = True Then
PlayerShip.Left -= 5
End If
If MoveUp = True Then
PlayerShip.Top -= 5 ' I use a timer to tick every 10 milliseconds ato check the states of each key, this statement controlls the execution of the direction
End If
If MoveDown = True Then
PlayerShip.Top += 5
End If
End Sub
End Class
However when i try to write an if statement to check if they will collide or not i do not get a '.bounds' option. I have seen '.bounds' being needed after the name of the picture box. Instead i have been met with '.setbounds' even though i have set them before.
Is something wrong with my version of visual studio, or am I missing something?

How can I disable the Page Down/Up, when the Focus in on a Button?

I have a program that can change values in a database. I have a TrueDBGrid in my form and the Page Down/Up works fine. If I click on a button with TAB, this Page Down/Up still works. How can I prevent this when the focus is on the button?
I've found a solution. That would be:
Private Sub frmZeitcodeListe_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Try
If e.KeyCode = Keys.PageUp Then
If cmdBearbeiten.Focused = True Or cmdLoeschen.Focused = True Or cmdNeu.Focused = True Or cmdSchliessen.Focused = True Then
e.Handled = False
Else
bsTblZeitcode.MovePrevious()
e.Handled = True
End If
ElseIf e.KeyCode = Keys.PageDown Then
If cmdBearbeiten.Focused = True Or cmdLoeschen.Focused = True Or cmdNeu.Focused = True Or cmdSchliessen.Focused = True Then
e.Handled = False
Else
bsTblZeitcode.MoveNext()
e.Handled = True
End If
End If
Catch ex As Exception
End Try
End Sub

Convert Sub to Class for multiple CurrTextBox's

I have been using the following Subs to check and ensure data entered into a TextBox is numeric and it then gets converted to currency decimals in a form with a single CurrTextBox, however I have now been tasked to create a form with multiple CurrTextBox's.
How would I convert the following code to handle multiple calls. I suppose I can create a Public Sub that the data entered into a particular CurrTextBox calls... Ideas?
Private Sub CurrBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) OrElse (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse e.KeyCode = Keys.Back Then
acceptableKey = True
Else
acceptableKey = False
End If
End Sub
' This sub handles the keypress inputs and ensures values are numeric only
Private Sub CurrBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
' Check for the flag being set in the KeyDown event.
If acceptableKey = False Then
' Stop the character from being entered into the control since it is non-numerical.
e.Handled = True
Return
Else
If e.KeyChar = Convert.ToChar(Keys.Back) Then
If strCurrency.Length > 0 Then
strCurrency = strCurrency.Substring(0, strCurrency.Length - 1)
End If
Else
strCurrency = strCurrency & e.KeyChar
End If
If strCurrency.Length = 0 Then
CurrBox2.Text = ""
ElseIf strCurrency.Length = 1 Then
CurrBox2.Text = "0.0" & strCurrency
ElseIf strCurrency.Length = 2 Then
CurrBox2.Text = "0." & strCurrency
ElseIf strCurrency.Length > 2 Then
CurrBox2.Text = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
End If
CurrBox2.Select(CurrBox2.Text.Length, 0)
End If
e.Handled = True
End Sub
One event can handle multiple controls.
Just add more controls after Handles clause in your events and all of these to be handled.
Private Sub CurrBox2_KeyDown(ByVal sender As Object, ByVal e System.Windows.Forms.KeyEventArgs) Handles currbox1.keydown, currbox2.keydown, currbox3.keydown
If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) OrElse (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse e.KeyCode = Keys.Back Then
acceptableKey = True
Else
acceptableKey = False
End If
End Sub
This way you need write those events only once.
I hope I was undertand your question right.

SendKeys not working in Datagridview keydown event

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

Control KeyDown Event Trap CTRL-C etc

I'm having a problem with processing CTRL-C, CTRL-V, CTRL-X on my form.
Private Function HandleKeyDown(sender As Object,
e As KeyEventArgs,
ByVal vShow As String) As Boolean
HandleKeyDown = False
If e.KeyCode = Keys.F1 Then
Help.ShowPopup(Me, vShow, Cursor.Position)
End If
If e.KeyCode = Keys.C AndAlso e.Modifiers = Keys.Control Then
sender.Copy()
ElseIf e.KeyCode = Keys.V AndAlso e.Modifiers = Keys.Control Then
sender.Paste()
ElseIf e.KeyCode = Keys.X AndAlso e.Modifiers = Keys.Control Then
sender.Cut()
Else
Console.WriteLine(String.Format("Modifiers:{0} KeyCode:{1} KeyData:{2} KeyValue:{3} ", e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
End If
HandleKeyDown = True
End Function
The KeyDown event never picks up the second key. The only value the KeyCode ever seems to hold is Keys.Control. This is what the Console.WriteLine outputs for CTRL-C
Modifiers:Control KeyCode:ControlKey KeyData:ControlKey, Control KeyValue:17
Where am I going wrong?
Are you calling this from the actual event handler? I had to add "Handles MyBase.KeyDown" and remove your extra parameter.
Private Sub HandleKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
'HandleKeyDown = False
If e.KeyCode = Keys.C AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show(String.Format("Modifiers:{0} --- KeyCode:{1} --- KeyData:{2} --- KeyValue:{3} ",
e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
'sender.Copy()
ElseIf e.KeyCode = Keys.V AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show(String.Format("Modifiers:{0} --- KeyCode:{1} --- KeyData:{2} --- KeyValue:{3} ",
e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
'sender.Paste()
ElseIf e.KeyCode = Keys.X AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show(String.Format("Modifiers:{0} --- KeyCode:{1} --- KeyData:{2} --- KeyValue:{3} ",
e.Modifiers.ToString, e.KeyCode.ToString, e.KeyData.ToString, e.KeyValue.ToString))
'sender.Cut()
End If
'HandleKeyDown = True
End Sub
It's working for me. I get a value of 88, 67, and 86 respectively.