Hotkey setting through a textbox. But key enum is combining and forming a new key

Smrithi Surendran 61 Reputation points
2022-12-22T11:10:29.167+00:00

I have a textbox where we will bind the key values and assign it as hotkey.

But key enum is combining and forming a new key

Please help here

For example,
When I click Alt and M it is showing sleep

Windows development | Windows App SDK
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Junjie Zhu - MSFT 21,646 Reputation points
    2022-12-23T09:50:11.213+00:00

    Hello @Smrithi Surendran ,
    Welcome to Microsoft Q&A!

    It is recommended that you set the enum constants to the power of 2.
    You can see official document code sample it also be power of 2.

    In your example the values look like this

    A = 0 0000
    B = 1 0001
    C = 2 0010
    D = 3 0011

    Key = Keys.B | Keys.C;
    B | C = 0011 which is 3 or D

    If you choose powers of 2, you will get

    A = 1 = 2^0 0001
    B = 2 = 2^1 0010
    C = 4 = 2^2 0100
    D = 8 = 2^3 1000

    You can refer to this question operator with enum values in C#

    Thank you.
    Junjie


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Junjie Zhu - MSFT 21,646 Reputation points
    2022-12-26T02:35:50.803+00:00

    It is recommended that you define the keys according to the power of 2.
    For example, if you have a shortcut key is D+G, if it means screenshot,
    then the definition of the screenshot key is Key= Keys.D | Keys.G
    Screenshot = Keys.D | Keys.G =72 = 0100 1000

    A = 1 = 2^0 0000 0001
    B = 2 = 2^1 0000 0010
    C = 4 = 2^2 0000 0100
    D = 8 = 2^3 0000 1000
    E = 16 = 2^4 0001 0000
    F = 32 = 2^5 0010 0000
    G = 64 = 2^6 0100 0000
    Screenshot=72
    H =128 = 2^7 1000 0000

    Similar to the definition in the official code sample

    // The example displays the following output:  
    //       All possible combinations of values without FlagsAttribute:  
    //         0 - None  
    //         1 - Black  
    //         2 - Red  
    //         3 - 3  
    //         4 - Green  
    //         5 - 5  
    //         6 - 6  
    //         7 - 7  
    //         8 - Blue  
    //         9 - 9  
    //        10 - 10  
    //        11 - 11  
    //        12 - 12  
    //        13 - 13  
    //        14 - 14  
    //        15 - 15  
    //        16 - 16  
    //  
    //       All possible combinations of values with FlagsAttribute:  
    //         0 - None  
    //         1 - Black  
    //         2 - Red  
    //         3 - Black, Red  
    //         4 - Green  
    //         5 - Black, Green  
    //         6 - Red, Green  
    //         7 - Black, Red, Green  
    //         8 - Blue  
    //         9 - Black, Blue  
    //        10 - Red, Blue  
    //        11 - Black, Red, Blue  
    //        12 - Green, Blue  
    //        13 - Black, Green, Blue  
    //        14 - Red, Green, Blue  
    //        15 - Black, Red, Green, Blue  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.