|
-
Jul 20th, 2015, 10:55 PM
#1
Thread Starter
Lively Member
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.
-
Jul 22nd, 2015, 02:15 AM
#2
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?
-
Jul 22nd, 2015, 08:10 AM
#3
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
-
Jul 22nd, 2015, 10:21 AM
#4
Thread Starter
Lively Member
Re: Let user customize keyboard shortcuts?
-
Jul 22nd, 2015, 07:19 PM
#5
Re: Let user customize keyboard shortcuts?
 Originally Posted by theryan722
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.
-
Jul 23rd, 2015, 02:16 AM
#6
Thread Starter
Lively Member
Re: Let user customize keyboard shortcuts?
 Originally Posted by jmcilhinney
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.
-
Jul 23rd, 2015, 05:03 AM
#7
Re: Let user customize keyboard shortcuts?
 Originally Posted by theryan722
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.
- Open your form in the designer
- Select your menu item
- Open the Properties window
- Expand the (ApplicationSettings) node
- Select the (PropertyBinding) node and click the browse (...) button
- Select the ShortcutKeys property and click the drop-down arrow
- Select (New...)
- 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.
- 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:
Dim sck = SomeToolStripMenuItem.ShortcutKeys
Dim parts As New List(Of String)
If (sck And Keys.Control) = Keys.Control Then
parts.Add("Ctrl")
End If
If (sck And Keys.Shift) = Keys.Shift Then
parts.Add("Shift")
End If
If (sck And Keys.Alt) = Keys.Alt Then
parts.Add("Alt")
End If
parts.Add((sck And Not (Keys.Control Or Keys.Shift Or Keys.Alt)).ToString())
MessageBox.Show(String.Join("+", parts))
How you get the input from the user is up to you.
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
|