Search Unity

shortcut keys script

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

  1. tree_arb

    tree_arb

    Joined:
    Dec 30, 2019
    Posts:
    323
    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;
    6.  
    7. public class UnityExtededShortKeys : ScriptableObject
    8. {
    9.  
    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

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
  3. tree_arb

    tree_arb

    Joined:
    Dec 30, 2019
    Posts:
    323
    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.

    example:
    shift+h or shift+g
    Code (CSharp):
    1. [MenuItem("HotKey/Stop (or play) #g")]
    2.  
    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..
     
  4. tree_arb

    tree_arb

    Joined:
    Dec 30, 2019
    Posts:
    323
    looking back at some old code i had figured this out back then i guess (see line 9 / 10).

    I'm not sure what other people do but this is so valuable to me not to have unity constantly pausing/ updating when i switch back and forth from code

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using UnityEditor.SceneManagement;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class UnityExtededShortKeys : ScriptableObject
    8. {
    9.     //alt+a
    10.     [MenuItem("HotKey/Play (with asset refresh) &a")]
    11.     static void PlayGame()
    12.     {
    13.         EditorSceneManager.SaveScene(SceneManager.GetActiveScene(), "", false);
    14.         EditorApplication.ExecuteMenuItem("Assets/Refresh");
    15.         EditorApplication.ExecuteMenuItem("Edit/Play");
    16.     }
    17.     [MenuItem("HotKey/Stop (or play) _s")]
    18.     static void StopOrPlay()
    19.     {
    20.         EditorApplication.ExecuteMenuItem("Edit/Play");
    21.     }
    22.     [MenuItem("HotKey/Refresh Assets _F5")]
    23.     static void DoRefresh()
    24.     {
    25.         EditorApplication.ExecuteMenuItem("Assets/Refresh");
    26.     }
    27.  
    28. }