Keypress in not read in visual basic 2010/2012 - vb.net

My app is based on Visual Basic 2010/2012 (all its codes are same for both the languages). I'm trying to do certain things when either the 'F' or 'G' is pressed from keyboard. I've tried all the things like using Keypress, keydown and keyup events but they all didn't work.
Here is a sample code of my app. This just shows which key was pressed by the user in a message box and this also doesn't work i.e. nothing happens, not even an error message.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If (e.KeyCode = Keys.Control AndAlso (e.KeyCode = Keys.F)) Then
MessageBox.Show("pressed F")
ElseIf (e.KeyCode = Keys.Control AndAlso (e.KeyCode = Keys.B)) Then
MessageBox.Show("pressed B")
End If
End Sub`

My take on optimizing the KeyDown handler, using DRY principle (=don't repeat yourself):
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If Not e.Control Then Exit Sub
Select Case e.KeyCode
Case Keys.F : MessageBox.Show("Ctrl-F")
Case Keys.B : MessageBox.Show("Ctrl-B")
End Select
End Sub

In keypressed
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
dim kc as string = e.KeyChar
if kc = "F" OR kc = "G" then msgbox "Horeeee"
End Sub

In addition to setting KeyPreview() to True for your Form, as already pointed out by #Tony Hopkinson, here is how to correctly check for Ctrl-F or Ctrl-B:
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Control AndAlso e.KeyCode = Keys.F Then
MessageBox.Show("Ctrl-F")
ElseIf e.Control AndAlso e.KeyCode = Keys.B Then
MessageBox.Show("Ctrl-B")
End If
End Sub

Related

VB disable "|" key

I'm trying to disable the | key in a textbox as my csv file uses the character as a separator. However, I tried many methods and searched on google but I can't find any solutions related to that.
My code:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If e.KeyCode = Keys.Shift And e.KeyCode = AscW(220) Then
e.SuppressKeyPress = True
End If
End Sub
Wonder if that is possible? Thanks
If you want to detect a key in combination with one or more modifiers then there are multiple ways to do it. You could do this:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If Not e.Control AndAlso e.Shift AndAlso Not e.Alt AndAlso e.KeyCode = Keys.Oem5 Then
e.SuppressKeyPress = True
End If
End Sub
or this:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Oem5 Then
e.SuppressKeyPress = True
End If
End Sub
but my preference would be this:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If e.KeyData = (Keys.Shift Or Keys.Oem5) Then
e.SuppressKeyPress = True
End If
End Sub
Note that that third snippet uses KeyData rather than KeyCode. The latter is just the current key while the former is the current key and any modifier keys.
If you want to make it a little more succinct:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
e.SuppressKeyPress = (e.KeyData = (Keys.Shift Or Keys.Oem5))
End Sub

VB.Net detect keydown on control before keydown on form event

I have enabled the KeyPreview option on the form and on the keydown of the form I am detecting the Escape keydown to popup a msgbox requesting whether the user wants to exit from the application.
But the problem is, I have one textbox that needs to clear its contents on Escape keypress rather than asking whether the user needs to exit. And I tried using the keydown event on this control but the form keydown event occurs before the control keydown (I want this to happen the other way around).
Code on the Textbox
Private Sub txtAmount(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
'//Escape keypress
MsgBox("TEXTBOX ESCAPE")
sender.Text = ""
End If
End Sub
Code used on the Main menu Form
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
I can't use the previewkeydown either because the MaskTextBox doesn't raise the previewkeydown event..
Check if txtAmount is focused in the Form_KeyDown:
Private Sub txtAmount_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
'//Escape keypress
MsgBox("TEXTBOX ESCAPE")
sender.Text = ""
End If
End Sub
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If txtAmount.Focused Then Return
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
Edit: Alternatively, if you don't want to specify each control individually, you could do it by type:
If TypeOf Me.ActiveControl Is MaskTextBox Then Return
Or even more simpler, change your Form_KeyDown-Event to a KeyUp Event.
Public Class frmMainMenu
Private KeyHandled As Boolean
Private Sub txtAmount_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
sender.Text = ""
KeyHandled = True
End If
End Sub
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
KeyHandled = False
End Sub
Private Sub frmMainMenu_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If KeyHandled Then Return
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
End Class
Here is my code
Private Sub When_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, MyBase.KeyDown
If sender.name = "Your form name" Then
If e.KeyCode = Keys.Escape And Not TextBox1.Focused Then
MsgBox("me")
End If
ElseIf sender.name = "your textBox name" Then
If e.KeyCode = Keys.Escape Then
MsgBox("txt")
End If
End If
End Sub
I hope this can help you

Keydown event weird behaviour

I'm trying to catch shortcut keys. I need an explanation on how KeyDown Events are managed. Let's take this as an example :
Private Sub SoldeOuvertFou_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control And e.KeyCode = Keys.W Then
MessageBox.Show("Ctrl+W")
End If
If e.Control And e.KeyCode = Keys.F5 Then
MessageBox.Show("Ctrl+F5")
End If
End Sub
Works perfectly. No matter which one I press first or how many times I press them both MessageBox will pop up. Now if I simply change the order inside the sub :
Private Sub SoldeOuvertFou_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control And e.KeyCode = Keys.F5 Then
MessageBox.Show("Ctrl+F5")
End If
If e.Control And e.KeyCode = Keys.W Then
MessageBox.Show("Ctrl+W")
End If
End Sub
With this approach, only Ctrl+F5 will pop-up. No way to make Ctrl+W appear... any idea why ?
you can simply use :
If e.Control Then
If e.KeyCode = Keys.F5 Then
MessageBox.Show("Ctrl+F5")
Else
If e.KeyCode = Keys.W Then
MessageBox.Show("Ctrl+W")
End If
End If
End If

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

vb.net key combination

I'm trying to capture two key presses in my VB.net application, for this example CTRL + B, the code below doesn't work but it does for single keys. I have tried setting keypreview as true but this has no effect.
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
If e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.B Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
End Class
Thanks
The Control key is a Modifier key. This code tests for Ctrl + B
e.KeyCode = Keys.B AndAlso e.Modifiers = Keys.Control
The key-code is B, but the modifier is Ctrl.
Your code snippet, updated:
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
If (e.KeyCode = Keys.B AndAlso e.Modifiers = Keys.Control) Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
You need to add controlkey also after modifier to make it work properly.
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
If (e.KeyCode = Keys.B AndAlso e.Modifiers.ControlKey = Keys.Control) Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
If you look at the documentation for KeyEventArgs, you'll note that the class exposes properties for ALT, CTRL and Modifiers which allow you to determine whether these keys were pressed in addition to the main symbol key you're interested in.
Private Sub main_KeyDown(
ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyEventArgs)
Handles mybase.KeyDown
If e.Control And e.KeyCode = Keys.B Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
You can use this:
If e.Control = True And e.KeyCode = Keys.B Then
'Do Something...
End If