Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

shortcut keys script

Discussion in 'Scripting' started by Treecrotch, Jan 14, 2021.

  1. Treecrotch


    Dec 30, 2019
    Hi, I've been using this I came across somewhere in the past to add shortcut keys.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using UnityEditor.SceneManagement;
    5. using UnityEngine.SceneManagement;
    7. public class UnityExtededShortKeys : ScriptableObject
    8. {
    10.     //edit the keys here
    11.     [MenuItem("HotKey/Play (with asset refresh) _a")]
    12.     static void PlayGame()
    13.     {
    14.         EditorSceneManager.SaveScene(SceneManager.GetActiveScene(), "", false);
    15.         EditorApplication.ExecuteMenuItem("Assets/Refresh");
    16.         EditorApplication.ExecuteMenuItem("Edit/Play");
    17.     }
    18.     [MenuItem("HotKey/Stop (or play) _s")]
    19.     static void StopOrPlay()
    20.     {
    21.         EditorApplication.ExecuteMenuItem("Edit/Play");
    22.     }
    23.     [MenuItem("HotKey/Refresh Assets _F5")]
    24.     static void DoRefresh()
    25.     {
    26.         EditorApplication.ExecuteMenuItem("Assets/Refresh");
    27.     }
    28. }
    This works well if I press a, s, or F5
    However i cant seem to figure out how to add key combinations. for example if I want the short cut to be a and s at the same time
    Code (CSharp):
    1.   [MenuItem("HotKey/Stop (or play) _s+_a")] // doesn't work
    it does show up in the hoykeys menu item that this creates, and it does show the shortcut as S + A, which it capitalizes so it seems to recognize it as correct. and it works if I press that menu item. but the keys dont work
  2. PraetorBlue


    Dec 13, 2012
  3. Treecrotch


    Dec 30, 2019
    thanks that helped a lot I was wondering how it even was working at all.

    I've come across some strange behavior.
    I made a few hotkeys with shift modifier + letters.

    It would not work, but would use the previous attempts like it was cached or something.

    shift+h or shift+g
    Code (CSharp):
    1. [MenuItem("HotKey/Stop (or play) #g")]
    3. [MenuItem("HotKey/Stop (or play) #h")]
    I get it working w shift+g and then which switched to shift+h, save, asset refresh...the menu shows correct shift+h and works if clicked from there...but it would still work with only the old shift+g on the keyboard..and then a few attempts later it would update correctly.

    thinking it was oddly delayed, something cached, I again try:
    Code (CSharp):
    1. [MenuItem("HotKey/Stop (or play) _a+_s")]
    unity does list it in the menu correctly showing as if those are the keys to use, but it does not work.

    so perhaps its limited like you said..