|
-
Aug 14th, 2013, 12:37 PM
#1
Thread Starter
New Member
HotKeys With Options
My code:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Get the settings
Label1.Text = String.Format("Hotkey1: {0}{1}Hotkey2:{2}", My.Settings.hotkey_combo1.ToString, Environment.NewLine, My.Settings.hotkey_combo2.ToString)
'Set up the combobox's properites
With ComboBox1
.Items.AddRange(New Object() {Keys.PrintScreen, Keys.F12})
End With
'Set up the combobox's properites
With ComboBox2
.Items.AddRange(New Object() {Keys.ControlKey, Keys.ShiftKey})
End With
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
My.Settings.hotkey_combo1 = DirectCast(ComboBox1.SelectedItem, Keys)
My.Settings.hotkey_combo2 = DirectCast(ComboBox2.SelectedItem, Keys)
End Sub
End Class
My problem:
How can i add ({Keys.Control + keys.t, Keys.Shift})
Code:
.Items.AddRange(New Object() {Keys.ControlKey + keys.t, Keys.ShiftKey})
When i press control + t to do something, not only control.
When i do this "Keys.Control + keys.t" everything is ok no error, but vb add value of control and value of t 
when i set + vb add this 2 value, when i set * vb multiply this 2 value and also i was try with 'add', 'or' but nothing
But when i have only one button everything is ok, but when i have combination of 2 buttons (controlkey + t) that works to but vb doesn't recognize that as hotkey or let's say button.
Thank you in advance!
-
Aug 14th, 2013, 07:02 PM
#2
Re: HotKeys With Options
There is a difference between detecting the user pressing the Ctrl key and the user pressing some other key with the Ctrl modifier. If you want to detect just the Ctrl key then you use Keys.ControlKey. If you want to detect another key with the Ctrl modifier then you use Keys.Control (NOT Keys.ControlKey) and you bitwise Or it with the other Keys value. For instance, if you want to represent Ctrl+T then you use 'Keys.Control Or Key.T'.
-
Aug 15th, 2013, 06:52 AM
#3
Thread Starter
New Member
Re: HotKeys With Options
Okey, now it works but there is another problem with detecting :S
This code is now okay.
Code:
.Items.AddRange(New Object() {Keys.Control Or Keys.T})
And now it looks okay to:
http://prntscr.com/1ldnjx
But when i press Control + T nothing happens
My detect code:
Code:
If GetKeyPress(My.Settings.hotkey_combo2) Then
MsgBox("Options 1")
Else
End If
My.Settings.hotkey_combo2 is:
Code:
.Items.AddRange(New Object() {Keys.Control Or Keys.T})
Saved to My.settings.
-
Aug 15th, 2013, 07:22 AM
#4
Re: HotKeys With Options
If you expect to detect key combinations using the Keys enumeration then you must handle a KeyDown event. Are you? Also, if you're handling the form's KeyDown event then you must set its KeyPreview property to True or else the event won't be raised when a child control has focus.
-
Aug 15th, 2013, 07:50 AM
#5
Thread Starter
New Member
Re: HotKeys With Options
i didn't work with that yet, please can you give me example or something? i would be grateful..
My code for now:
Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
KeyPreview = True
End Sub
-
Aug 15th, 2013, 07:54 AM
#6
Re: HotKeys With Options
It's no use setting KeyPreview to True in the KeyDown event handler because the event will never be raised if the property is not True to start with. That's like expecting to be able to set your alarm clock when the alarm rings; it ain't going to ring if you don't set it first. You set the property in the designer, just like you do with other form properties, and you test for the desired key combination in the event handler.
Have you read anything about that event? I'll wager not, so it' snot really a surprise that you can't use it properly. Start by reading the MSDN documentation for that event and you would also benefit by following the Blog link in my signature and checking out my post on Keyboard Events.
-
Aug 15th, 2013, 09:17 AM
#7
Thread Starter
New Member
Re: HotKeys With Options
I know what are you mean, but i don't know how can i detect pressed button,
I need something like this:
Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = My.Settings.hotkey_combo2 Then
'do...
End If
End Sub
i know that i can make that like this:
Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.T Then
MsgBox("lalala")
End If
End Sub
but i wanna make options that users can choose between several hotkey options,
let's say: Control + T or Shift + s or Alt + t and i need store their options to my.settings and after that i need detect which options is user choose and detect if user press that buttons..
-
Aug 15th, 2013, 11:04 AM
#8
Re: HotKeys With Options
Suggestion for structure ...
vb.net Code:
Public Class Form1
' declare all hotkey combos
Dim kcAltB As Integer = Keys.Alt Or Keys.B
Dim kcShiftM As Integer = Keys.Shift Or Keys.M
' declare all hotkey action triggers
Dim DoSomethingClever As Integer
Dim DoSomethingReallyClever As Integer
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' assign values to triggers according to selected hotkey combos eg.
DoSomethingClever = kcShiftM
DoSomethingReallyClever = kcAltB
' or eg. DoSomethingClever = My.Settings.Clever
End Sub
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
' detect the key presses
Select Case e.KeyData ' NB Not .KeyCode as it does not detect multi-key input
Case CType(DoSomethingClever, Keys)
' do something clever
Case CType(DoSomethingReallyClever, Keys)
' do something really clever
Me.Text = "Aren't I clever?"
End Select
End Sub
End Class
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Aug 15th, 2013, 06:42 PM
#9
Re: HotKeys With Options
Did you bother to read the blog post that I specifically directed you to? I explain how to do this and even provide an example in that post. I created that post specifically so this wouldn't have to be explained over and over but it won't help if you don't read it.
-
Aug 16th, 2013, 04:06 AM
#10
Thread Starter
New Member
Re: HotKeys With Options
But where is that link? i didn't see it..
-
Aug 16th, 2013, 04:09 AM
#11
Re: HotKeys With Options
 Originally Posted by Budo
But where is that link? i didn't see it..
So why didn't you say that instead of simply ignoring the suggestion? As I said in my earlier post, it's in my signature, i.e. the boiler-plate text that appears at the bottom of each of my posts. The link even says "Keyboard Events", exactly as I posted. I do have to wonder how hard you looked. Anyway, you have it now... I hope.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|