Get Keys from a ShortCut

后端 未结 3 814
独厮守ぢ
独厮守ぢ 2020-12-21 12:34

Is there another way of getting the Keys from a Shortcut besides

sc is of type System.Windows.Forms.Shortcut

var k = (Keys)sc;
         


        
3条回答
  •  没有蜡笔的小新
    2020-12-21 13:01

    The ShortCut enum values were already carefully chosen to be an exact match with the Keys enumeration for the short-cut. For example, ShortCut.CtrlShiftF1 is 0x30070 which matches (Keys.Control | Keys.Shift | Keys.F1): 0x20000 | 0x10000 | 0x00070 = 0x30070. This was not an accident.

    Converting the ShortCut to a string is already provided, a menu item in MenuStrip can automatically display the string of the MenuItem.Shortcut if you set its ShowShortcut property to True. You can use the same technique in your own code, use the KeysConverter class:

        var sc = Shortcut.CtrlShiftF1;
        var txt = new KeysConverter().ConvertToString((Keys)sc);
        Console.WriteLine(txt);
    

    Output:

    Ctrl+Shift+F1 .

提交回复
热议问题