Lieven Cardoen
Lieven Cardoen

Reputation: 25969

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.

Upvotes: 2

Views: 839

Answers (3)

lievendf
lievendf

Reputation: 90

Example in ABL:

USING Progress.Util.TypeHelper FROM ASSEMBLY.
USING System.Enum FROM ASSEMBLY.
USING System.Windows.Forms.Keys FROM ASSEMBLY.
USING System.Windows.Forms.Shortcut FROM ASSEMBLY.

DEFINE VARIABLE ShortCut AS ShortCut NO-UNDO.
DEFINE VARIABLE Keys_ AS Keys NO-UNDO.

ShortCut = System.Windows.Forms.Shortcut:CtrlShiftF1.
Keys_ = CAST(Enum:ToObject(TypeHelper:GetType("System.Windows.Forms.Keys"), ShortCut:value__), Keys).

MESSAGE Keys_
    VIEW-AS ALERT-BOX.

Upvotes: 1

Mike Fechner
Mike Fechner

Reputation: 7192

I have solved this using the ABL .NET bridge by comparing the result of the System.Windows.Forms.Shortcut's value GetHashValue() to the e:KeyData:GetHGashValue() in an KeyDown event handler.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942207

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 .

Upvotes: 5

Related Questions