How would I do the following in Visual Basic Express?
a) Press "delete", "home", and "shift" on the keyboard with the program. // Still need to figure out how to do this.
b) Detect when "z" and "x" are pressed. // I'm using buttons instead of this part now.
Thanks so much! :)
Windows Form Application
Here is "ONE" way.. it detects the Enter press in .NET Win Forms. The 13 represents "Enter".
Public Function KeyAscii(ByVal UserKeyArgument As KeyPressEventArgs) As Short
KeyAscii = Asc(UserKeyArgument.KeyChar)
End Function
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If KeyAscii(e) = 13 Then
MsgBox("you press ENTER key")
End If
End Sub
More key types can be found by using something like this to detect your key presses.
Private Sub Form_KeyPress(KeyAscii As Integer)
Debug.Print "KeyAscii: " & KeyAscii
End Sub
Private Sub Form_Load()
Form1.KeyPreview = True
End Sub
I think the SendKey function would meet your needs.
a) To send keyboard commands, you can use this SendKeys method.
b) To capture keystrokes check out this support article.
Related
If you hold down the key in a KeyDown Sub, it repeats the code until it is released.
Is there any way to prevent the code from continuously running and keep it so that it only runs once? Thanks.
You need to handle more than one key events to do that. For example
Public Class Form1
Private keyHolding As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If Not keyHolding Then
Label1.Text &= "Keydown event detected "
keyHolding = True
'Place the code that you want to run only once in the key down event here...
Else
Label1.Text &= "User is holding the key down "
'Place the code that you want to run continuously in the key down event here...
End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Label1.Text &= "KeyUp event detected "
keyHolding = False
End Sub
End Class
Just keep in mind that this approach is good for standard windows forms applications. If you are developing a game, for example, then this approach will cause various problems and there are better solutions either via native API calls or some game developing framework.
Hope this helps.
So I'm currently designing my A-level computing project, and I need to know if VB.NET can register multiple keypresses at the same time, e.g F&J, and be able to treat them as separate keypresses. I may need anywhere up to 4 keypresses at once so if VB.NET can't do it, my program will be limited (though only slightly).
If this is possible, do i just treat it as if they weren't pressed at the same time and check for both keys individually, or is there a special way of detecting this?
Thanks in advance.
You can keep track of the keys you have pressed, and released.
Create a new winforms project and add a Label. This should give you a good starting point.
Public Class Form1
Private pressedKeys As New List(Of System.Windows.Forms.Keys)()
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If Not pressedKeys.Contains(e.KeyCode) Then pressedKeys.Add(e.KeyCode)
printCurrentKeys()
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
If pressedKeys.Contains(e.KeyCode) Then pressedKeys.Remove(e.KeyCode)
printCurrentKeys()
End Sub
Private Sub printCurrentKeys()
If pressedKeys.Count > 0 Then
Me.Label1.Text = pressedKeys.
Select(Of String)(Function(k) Chr(k)).
Aggregate(Function(s1, s2) s1 & ", " & s2)
Else
Me.Label1.Text = ""
End If
End Sub
End Class
(The above 8 keys made possible by my anti-ghosting keyboard, the Sidewinder X4.)
Supposedly you can check in the registry
Dim KeyboardDataQueueSize = My.Computer.Registry.GetValue(
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Kbdclass\Parameters",
"KeyboardDataQueueSize", Nothing)
https://www.google.com/search?q=keyboard+buffer+size
I am trying to make a program which needs you to select a hotkey, but you can only give a character from the code I've produced, where as I want to be able to set something like F9 as well. I have been through a few on this site and this is the closest to what I want. I'm using Visual Studio 2013.
This is what I've got so far.
Private Sub Configure_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
MsgBox("Your new hotkey is: " & e.KeyChar)
End Sub
Thanks in advance.
The KeyPress event is not raised by non-character keys other than space and backspace; however, the non-character keys do raise the KeyDown and KeyUp events.
Set ReadOnly property of your TextBox1 to true and handle keyDown event:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
Me.TextBox1.Text = e.KeyData.ToString()
End Sub
This shows also the key combinations in te text box, for example if you press Ctrl + Shift + F9 you will see F9, Shift, Control in text box.
You can use KeyCode, Modifiers and other properties of KeyEventArgs to gain information about keys.
I am trying to use VB.net to answer and end Skype calls on button presses. I have tried this:
Public Sub oSkype_CallStatus(pCall As SKYPE4COMLib.Call, Status As
SKYPE4COMLib.TCallStatus) Handles oSkype.CallStatus
If Status = SKYPE4COMLib.TCallStatus.clsInProgress Then
pCall.Finish()
End If
End Sub
Which works great but I'm trying to end a call on a button press and not depending on the call status. I have tried calling this method when a button is pressed, but I am not sure what to put as the parameter for pCall.
Make a boolean that changes value when you press the button. :)
Dim PressedEnd As Boolean = False
Private Sub ButtonEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnd.Click
PressedEnd = True
End Sub
Public Sub oSkype_CallStatus(pCall As SKYPE4COMLib.Call, Status As SKYPE4COMLib.TCallStatus) Handles oSkype.CallStatus
If Status = SKYPE4COMLib.TCallStatus.clsInProgress AndAlso PressedEnd = True Then
PressedEnd = False
pCall.Finish()
End If
End Sub
Same goes for answering.
Skype is no longer supporting this API so it has lost some functionality. In order to end and answer calls I am sending keys via c# to Skype the corresponds to the hot keys that I have set in Skype. So I set Alt PGUP to answer a call and in code I send
SendKeys.SendWait("%(PGUP)");
Currently I have the start-key for my vb.net application hardcoded like this:
GetAsyncKeyState(Keys.F2)
Where vb.net sais "F2 As System.Windows.Forms.Keys = 113" on mouse-over
But I want my users to be able to pick their own Key. If I make a drop-down box (combobox) and pre-define some choices in there (Like ESC or F3), all those choices are strings. How can I convert those strings to a System.Windows.Forms.Keys integer?
Also, I'd like it to also be possible to "capture" a single keypress. So they'd click the "capture" button, and the next key they hit will be saved as the start/stop button. But I wouldn't even know where to begin looking for that one.
If txtKeys.Text=="F3" Then
GetAsyncKeyState(Keys.F3)
End If
Try something like this:
Public Class Form1
Dim captureKey As Boolean
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
captureKey = True
End Sub
Private Sub Button1_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown
If captureKey Then
Label1.Text = e.KeyValue.ToString
captureKey = False
End If
End Sub
End Class
I created a form with a label and a button for an example. e.KeyValue is an integer that I am converting to a string for display purposes. You also have the ability to capture other keydata. See this info on PreviewKeyDownEventArg
As for the first part of your question use a Select Case Statement to convert between your ComboBox Values and KeyData Values.