Results 1 to 7 of 7

Thread: Let user customize keyboard shortcuts?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2012
    Posts
    70

    Let user customize keyboard shortcuts?

    I want to let the user of my application be able to customize the keyboard shortcuts at run time. For example, I have it the keyboard shortcut CTRL+C as the copy command, but say my user wants to set it to CTRL+SHIFT+C. I am currently using the shortcut let property of the menustrip to use keyboard shortcuts. I already have an overly complex idea in mind, but I want to know if there is a simpler way to do this. I tried googling but couldn't find anything. Thanks.

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

    Re: Let user customize keyboard shortcuts?

    No. No, no, no. Ctrl+C is a system-wide keyboard shortcut for copying data to the Clipboard. It works throughout Windows and has done for decades. Why would you think that it's a good idea to let the user change that for your one application?

    If you want to let the user create/modify keyboard shortcuts for application-specific tasks then knock yourself out. You could even let them add another one for Copy, although why anyone would consider that a good thing is beyond me. You could also probably write code to override system actions like Copy but, again, why would you?
    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
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Let user customize keyboard shortcuts?

    I think the Ctrl+C was just an example. Using CTRL+J to open the JavaScript console and wanting to re-map it to CTRL+SHIFT+J instead might have been a better example as it isn't tied to a basic computer function. It's just like the keyboard options in VS.

    As for addressing how to do it... I'm not sure what the best/expedient way to do so is. I guess it kind of depends on the type of application and how it is designed. It sounds like you have something workable, as long as you have a menu item you can tie the shortcut to.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2012
    Posts
    70

    Re: Let user customize keyboard shortcuts?

    It was just an example.

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

    Re: Let user customize keyboard shortcuts?

    Quote Originally Posted by theryan722 View Post
    It was just an example.
    Well, it was probably the very worst example you could have used. It's not YOU that has Ctrl+C set as the keyboard shortcut for Copy; it's Windows that has had it set to that for decades.

    So, what is it that actually want to do? Your question is actually too vague to provide a specific solution. Are you talking about ShortcutKeys properties of ToolStripMenuItems specifically or are you handling keyboard events of forms or other controls to detect specific key combinations? The situation is similar in either case because ShortcutKeys is type Keys and you would store and detect Keys values in the second case too. With regards to a UI, maybe you should take a look at how VS does it for the ShortcutKeys property.
    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2012
    Posts
    70

    Re: Let user customize keyboard shortcuts?

    Quote Originally Posted by jmcilhinney View Post
    Well, it was probably the very worst example you could have used. It's not YOU that has Ctrl+C set as the keyboard shortcut for Copy; it's Windows that has had it set to that for decades.

    So, what is it that actually want to do? Your question is actually too vague to provide a specific solution. Are you talking about ShortcutKeys properties of ToolStripMenuItems specifically or are you handling keyboard events of forms or other controls to detect specific key combinations? The situation is similar in either case because ShortcutKeys is type Keys and you would store and detect Keys values in the second case too. With regards to a UI, maybe you should take a look at how VS does it for the ShortcutKeys property.
    I want to set up a system to allow the user to customize the keyboard shortcuts for the application, i.e. I have a menu item that says "Display Dialog" and the shortcut is Ctrl+Shift+J. I want the user to be able to map their own keyboard shortcut to this, say Ctrl+J and then from now on it will be Ctrl+J.

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

    Re: Let user customize keyboard shortcuts?

    Quote Originally Posted by theryan722 View Post
    I want to set up a system to allow the user to customize the keyboard shortcuts for the application, i.e. I have a menu item that says "Display Dialog" and the shortcut is Ctrl+Shift+J. I want the user to be able to map their own keyboard shortcut to this, say Ctrl+J and then from now on it will be Ctrl+J.
    So then, what you care about is setting the ShortcutKeys property of a ToolStripMenuItem, which you should already know because you must have already set it to Ctrl+Shift+J in the designer. You'll need to store the value external to the application so that any changes are persisted between sessions and that functionality is already built in.

    1. Open your form in the designer
    2. Select your menu item
    3. Open the Properties window
    4. Expand the (ApplicationSettings) node
    5. Select the (PropertyBinding) node and click the browse (...) button
    6. Select the ShortcutKeys property and click the drop-down arrow
    7. Select (New...)
    8. The DeafultValue should already be set. Enter a name for the setting that will persist the value. For a menu item called SomethingMenuItem I would suggest a name of SomethingMenuItemShortcutKeys.
    9. Click OK and OK again


    That's it. You can now change the ShortcutKeys property of your menu item and the new value will be automatically saved to the user config file at shutdown and loaded again at startup.

    When the user wants to change the key combination, you have to gather their input and convert it to a Keys value if required, then assign that Keys value to the ShortcutKeys property. For instance, Ctrl+Shift+J would be (Keys.Control Or Keys.Shift Or Keys.J). You can separate the composite values for display like this:
    vb.net Code:
    1. Dim sck = SomeToolStripMenuItem.ShortcutKeys
    2. Dim parts As New List(Of String)
    3.  
    4. If (sck And Keys.Control) = Keys.Control Then
    5.     parts.Add("Ctrl")
    6. End If
    7.  
    8. If (sck And Keys.Shift) = Keys.Shift Then
    9.     parts.Add("Shift")
    10. End If
    11.  
    12. If (sck And Keys.Alt) = Keys.Alt Then
    13.     parts.Add("Alt")
    14. End If
    15.  
    16. parts.Add((sck And Not (Keys.Control Or Keys.Shift Or Keys.Alt)).ToString())
    17.  
    18. MessageBox.Show(String.Join("+", parts))
    How you get the input from the user is up to you.
    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