Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

c# - Get Keys from a ShortCut

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

sc is of type System.Windows.Forms.Shortcut

var k = (Keys)sc;

I need the separate strings for each of the keys and the above won't work since I'm using a Progress ABL .NET bridge (don't ask).

I thought sc should be an integer, but apparently in .NET this line of code works fine.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...