I'm trying to create a custom editor script on Unity, and I'd like to have quite a few shortcuts for different reasons.
Is there any way to create shortcuts that trigger an event in the Editor? Creating a custom editor just to edit the shortcuts of the first editor is impossible for me since I don't have that much time, but it would really help me to be able to change my own shortcuts easily.
As of now this is my code:
static void EditorGlobalKeyPress()
{
Event current = Event.current;
if ((!current.alt && !current.control)|| current.type != EventType.KeyUp) return;
switch (Event.current.keyCode)
{
case KeyCode.L:
// Load stuff
break;
case KeyCode.U:
// Unload stuff
break;
default:
break;
}
}
And while it works fine, having editable shortcuts would be great. Does anyone know if it's possible in some way?
Yes you can do this using a MenuItem
To create a hotkey you can use the following special characters:
% (ctrl on Windows and Linux, cmd on macOS),
^ (ctrl on Windows, Linux, and macOS),
# (shift),
& (alt).
If no special modifier key combinations are required the key can be given after an underscore. For example to create a menu with hotkey shift-alt-g use "MyMenu/Do Something #&g". To create a menu with hotkey g and no key modifiers pressed use "MyMenu/Do Something _g".
Some special keyboard keys are supported as hotkeys, for example
"#LEFT" would map to shift-left. The keys supported like this are:
LEFT, RIGHT, UP, DOWN, F1 .. F12, HOME, END, PGUP, PGDN, INS, DEL, TAB, SPACE.
A hotkey text must be preceded with a space character ("MyMenu/Do_g" won't be interpreted as hotkey, while "MyMenu/Do _g" will).
For example
[MenuItem("Test/example %g")]
private static void TEEEEST()
{
Debu.Log("Example!");
}
This also adds a global shortcut
Also from a UX perspective this is better so a user can not only do this via the shortcut but also via the menu. And you can look up why things are happening when you press the shortcut buttons (see below) ;)
And then any user can still freely edit and reassign them later via the Shortcut Manager (Edit -> Shortcuts...)
Related
I have two text-boxes, one for English and one for Russian. For ease of use purposes, I'd like to know if there's something available in c# that tells my system to switch to one of the installed keyboard layouts.
I would then plan to set a method that does this as soon as one of the text-boxes get focused.
So when the Russian box is focused, the windows Russian keyboard layout is used and vice versa.
I was searching online for a bit but I didn't find any of the sort. Since I wanted it finished early I did a workaround and just simulated the key-presses necessary to switch keyboard layouts on windows using Input-simulator. Now I am looking for a better solution.
Public Form1()
{
// I use the method when either of the text-boxes are used.
// When I find a better solution, there will obviously be two separate methods
txtRussian.GotFocus += SwitchKeyboard;
txtEnglish.GotFocus += SwitchKeyboard;
}
private void SwitchKeyboard(object sender, EventArgs e)
{
// shift alt for keyboard layout switch
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT,VirtualKeyCode.LMENU);
// LMENU (Left Alt) tends to still be pressed after you he finished the modified keystroke.
// So that makes any first key the user presses be the "LAlt + {a key}" instead of just a key.
// By normally simulating its press again the issue is gone
sim.Keyboard.KeyPress(VirtualKeyCode.LMENU);
}
Of course, this isn't what I'd truly want, cause whenever you alt tab in and out and refocus on a text-box, it'll just switch to the next keyboard layout installed instead of a specified keyboard layout for which the text-box is meant.
So yeah, is there a way to switch to a specified windows keyboard layout with c#?
There is the "InputLanguage" class in the System.Windows.Forms namespace.
You could use the "InputLanguage.CurrentInputLanguage" property to change the currently used keyboard layout.
There is already another post on stackoverflow about that:
C# - Automatically switch between two different IME in the same keyboard layout
However, you can only change the input language with that, but not the layout inside the language. But I think changing the input language is what you need.
If you also need to change the input layout of the language you could do so with setting the .ImeMode property of the TextBox.
The goal is simple: When the user enters chars in a textbox, I want to detect if this char is the question mark key (?).
I don't care what to use (text changed, key down etc...)
One thing to mention: I am working on a german keyboard layout and therefore I need a solution independent from the keyboard (for example: e.Key = Keys.OemQuestion isn't working, it fires when I press the plus (+) key)
Edit: I tried Convert.toString((char)e.Key) which returned \u0095 and e.Key.ToString() which returned OemOpenBrackets
I chose the solution from #HansPassant and managed to do it with the TextInput event.
First in the constructor:
InitializeComponent();
CommandTextBox.AddHandler(TextBox.TextInputEvent, new TextCompositionEventHandler(CommandTextBox_TextInput), true);
You need this code to actually fire the event
in TextInput
if(e.Text == "?")
{
//Do something
}
NOTE:
This does not capture space, control, shift etc.
I have a strange problem. I have a toolStripMenuItem that I wish to have the first letter underlined. This is in Windows Forms VS 2010.
It is defined in the designer with text equal to "&Expand" and show shortcut keys = true and shortcut keys = Alt+E. It shows up in the form in designer as expected with the E underlined.
I have a click event that changes the name, and then does something. If you click again it sets the name back
private void expandToolStripMenuItem_Click(object sender, EventArgs e)
{
if (expandToolStripMenuItem.Text == "&Expand")
{
expandToolStripMenuItem.Text = "Collaps&e";
expandToolStripMenuItem.ShortcutKeys = Keys.Alt | Keys.E;
// execute some other code
}
else
{
expandToolStripMenuItem.Text = "&Expand";
expandToolStripMenuItem.ShortcutKeys = Keys.Alt | Keys.E;
}
}
I also set the &Expand in the form shown and load events although this makes no difference.
Now when the form opens the E is not underlined. If I click it with the mouse it shows Collapse but the e is not underlined. I can go back and forth with the mouse and the letters are never underlined. However if I type AltE, the letter is underlined in both cases from that point on. Even with the mouse.
Any ideas?
Thanks
After my comment, I looked a little deeper. This is expected functionality and is configurable by the user.
In the Windows Control Panel in the Ease of Access Center, there is an option in the "Make the keyboard easier to use" section called "Make it easier to use keyboard shortcuts: Underline keyboard shortcuts and access keys". If that check box is unchecked the system will only underline access keys (the proper name for the shortcut underlining which you're dealing with) when the user is already navigating with access keys (using Alt). If the check box is checked then they will always be underlined.
You can test this with the file menu in pretty much any application in Windows.
I am trying to set my own hotkey combination for the action Edit.FindNext in visualstudio 2008 under:
Tools.Customize -> Keyboard
but these simply dont take effect, so as soon as i close the dialog my VS keeps working with the default combination: Ctrl + F3, and the one i set is just ignored, although its still set in the configuration.
restart didnt help.
Any ideas?
Thank you.
I found the problem,
i tried to set the Ctrl + Left Arrow for the find next action but the Left-Arrow can not be used in global as microsoft states:
The following keys cannot be assigned
to a command in Global: PRINT SCRN/SYS
RQ, SCROLL LOCK, PAUSE/BREAK, TAB,
CAPS LOCK, INSERT, HOME, END, PAGE UP,
PAGE DOWN, Windows logo keys,
Application key, any of the ARROW
keys, or ENTER; NUM LOCK, DEL, or
CLEAR on the numeric keypad; or
CTRL+ALT+DELETE.
so i had to set this to the texteditor context, and it worked.
Setting a hotkey in VS can be tricky. Be aware of the following: The same hotkey can be used for different things, depending on the context.
For example, it is possible, that you defined the hotkey for "Global", but for "Text Editor" your default hotkey is still registered and the new one might even be registered for something else in the context of "Text Editor". So when you enter your new hotkey, check out the Combobox "Shortcut currently used by", maybe it will show you what the problem is.
I want the 'Alt' to be pressed by code. It is like firing key-press event with key 'Alt' by the code, not hitting it manually. The need is, I have set 'ShortCut keys for menu, but it (the single underline on key letter) is not visible to the user unless he presses 'alt'. So i need to make the Alt be pressed by default.
Is there a way to 'press' or 'fireup' the keys in key board using c# code?
Check out the System.Windows.Forms.SendKeys class.
You can use the static Send method to send keystrokes to the active window. If you're trying to send keystrokes to another window, you'll need to use the Windows API to activate the other window first.
If you have any control over the operating system on which the program is being deployed, apparently you can force the underlined shortcut letter to always be displayed by going to Control Panel -> Display -> Appearance -> Effects -> Hide underlined letters for keyboard navigation.
(http://www.chinhdo.com/20080902/underlined-letters-windows/)
here is a complete article on use of SendKeys on codeproject
Here is how you simulate input (both mouse and keyboard).
http://msdn.microsoft.com/en-us/library/ms171548.aspx
If you look at the System.Windows.Forms.SendKeys class you will see that it provides you with what you want.
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx