Get Key Pressed in Variable - Visual Studio Basic 2017 - vb.net

I am trying to make a simple program, pasting in whatever you write in a textbox. I am doing this in Visual Studio 2017. It is a Windows-Forms App with Visual Basic. It works in the current state, but I woud like to add the ability to customize the key. Right now, when you press F12, it sends the message. I want a simple way of asking the user for a key, and after he presses it, it gets set as the new paste key. Any idea how I can accomplish this? Thanks in advance.
My code below, where I have Keys.F12 I have/want a variable that holds the key set by the user.
Dim hotkey As Boolean
hotkey = GetAsyncKeyState(Keys.F12)
If hotkey = True Then
SendKeys.Send(TextBox1.Text)
End If

I guess this is what you're looking for:
Private myNewKey As Char
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Convert.ToChar(myNewKey) Then
e.Handled = True ' to prevent getting the Press Key to be entered
TextBox1.Text = Clipboard.GetText
End If
End Sub
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
myNewKey = e.KeyChar
Label1.Text = "New paste key set to: " + e.KeyChar.ToString
End Sub
This program changes the pasting key each time user changes it by their own wish. Just simply declare a variable which holds the new specified key and when the user will get asked to enter a new press key, the variable should be assigned to it.
Eventually, when the user hits the same key, the TextBox1 KeyPress event will be triggered and the TextBox's text will be set by Clipboard.GetText.
Key set to 5 and when user hits that during focus on TextBox1 (second box), the text will get pasted.

If e.KeyCode = Keys.F12 Then
SendKeys.Send(TextBox1.Text)
End If

Related

How to check the key pressed in VB

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.

How to enable overwrite text in a textbox?

I'm making a program which requires a login system. What I want to do is have a textbox which contains the word 'Username' and when the user clicks it, the 'Username' text is overwritten. For example, Spotify use it on their login screen:
My question is, how do I do this?
Set the Text property of the TextBox that you are using for the username to the string "Username". Then, in the TextBox's Click event, change it to a blank string. Like so:
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
TextBox1.Text = ""
End Sub
Edit:
As #LarsTech mentioned, this does not address if the user tabs into the TextBox. If you wanted to account for that too, use the TextBox's Enter event instead:
Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
TextBox1.Text = ""
End Sub
I agree, using Textbox1.Enter is the easiest solution. On top you can also catch the case of no text entered via TextBox1.Leave like that
Private Sub TextBoxLeaveHandle() Handles TextBox1.Leave
If TextBox1.Text = "" Then
TextBox1.Text = "Username"
End If
End Sub
Sometimes it can also be useful to use the TextBox.SelectAll() function as it not immediately remove the entire text but (obviously from the name) select the entire text so you can overwrite it with your first keypress.

Single code for Tab Key functionality using Enter Key in Vb.Net for a Windows Application

I write this code for tab-like behavior when user presses ENTER on textboxes for every form, which works fine.
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
However, I need to write the code once, perhaps as a sub or function in a module so that I do not have to write the same for every form. I have checked various forums including
Detecting Enter keypress on VB.NET and
Tab Key Functionality Using Enter Key in VB.Net
BUT all I get is either to write code for each textbox or for every individual form.
Has anyone tried out a single code for ALL or may be several selected forms of the application? If yes, please share with me. Writing for every form still works fine for me but i need to take advantage of OOP. Thanks
There are many ways to implement this, one of those is to create a custom textbox User Control
Adding a control to your project is very easy just right click in your project in the solution explorer ->Add->User Control
Give a name to your control ex. "tabedtextbox"
Add a textbox control to your User Control
Place the code in the keypress event of textbox1 of the UserControl
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
Compile once in order to update the whole project with the new user control.
Goto to your form and add your new User Control, you can locate it at the Toolbox panel.
Run your program and you will see the behavior of TAB when you press enter on each textbox.
If You wish to allow the user to press enter in a TextBox, instead of pressing a specific button,
you can use the acceptbutton property.
One good way to use it, is to change the property, on the Enter and Leave events of the TextBox. That you call Click event of the Button, when User press Enter inside the TextBox.
Try this code:
Private Sub tbProject_Enter(sender As Object, e As EventArgs) Handles tbProject.Enter
Me.AcceptButton = bSearch
End Sub
Private Sub tbProject_Leave(sender As Object, e As EventArgs) Handles tbProject.Leave
Me.AcceptButton = Nothing
End Sub
Private Sub bSearch_Click(sender As Object, e As EventArgs) Handles bSearch.Click
'.... actions to perfom
End Sub
Screen

How do I auto-indent new lines using a Windows 8 RichEditBox?

I have the following VB code to add a Tab character whenever the key is pressed within the RichEditBox, but I can't figure out how I can make it so when an end user presses the return key, the app will auto-indent so you wouldn't need to press the Tab key however many times to nest code correctly on each new line.
Any help is appreciated, thanks.
Current code for Tab insertion on KeyDown:
Private Sub TextBox_KeyDown(sender As Object, e As KeyRoutedEventArgs) Handles TextBox.KeyDown
If e.Key = Windows.System.VirtualKey.Tab Then
e.Handled = True
Dim SelectionText As String = ""
TextBox.Document.Selection.GetText(Windows.UI.Text.TextGetOptions.None, SelectionText)
TextBox.Document.Selection.TypeText(vbTab + SelectionText)
End If
End Sub

Capture keypress / let user pick their own start/stop key

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.