Results 1 to 11 of 11

Thread: HotKeys With Options

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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'.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

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

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: HotKeys With Options

    Suggestion for structure ...

    vb.net Code:
    1. Public Class Form1
    2.     ' declare all hotkey combos
    3.     Dim kcAltB As Integer = Keys.Alt Or Keys.B
    4.     Dim kcShiftM As Integer = Keys.Shift Or Keys.M
    5.  
    6.     ' declare all hotkey action triggers
    7.     Dim DoSomethingClever As Integer
    8.     Dim DoSomethingReallyClever As Integer
    9.  
    10.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    11.         ' assign values to triggers according to selected hotkey combos eg.
    12.         DoSomethingClever = kcShiftM
    13.         DoSomethingReallyClever = kcAltB
    14.  
    15.         ' or eg. DoSomethingClever = My.Settings.Clever
    16.     End Sub
    17.  
    18.     Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    19.         ' detect the key presses
    20.         Select Case e.KeyData ' NB Not .KeyCode as it does not detect multi-key input
    21.             Case CType(DoSomethingClever, Keys)
    22.                 ' do something clever
    23.  
    24.             Case CType(DoSomethingReallyClever, Keys)
    25.                 ' do something really clever
    26.                 Me.Text = "Aren't I clever?"
    27.         End Select
    28.  
    29.     End Sub
    30.  
    31. 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!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    7

    Re: HotKeys With Options

    But where is that link? i didn't see it..

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: HotKeys With Options

    Quote Originally Posted by Budo View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width