0

I'm making a program to have user customizable hotkeys and I have pretty much everything working except for 1 hitch converting the modifier keys to int32 using gethashcode()

private void hotBox1_KeyDown(object sender, KeyEventArgs e)
{
    string _senderName = ((TextBox)sender).Name;
    int _sender_id = Convert.ToInt32(_senderName.Split(new[] { "hotBox" },
                                     StringSplitOptions.None)[1]) - 1;

    Keys modifierKeys = e.Modifiers;
    Keys pressedKey = e.KeyData ^ modifierKeys;

    /*
     * have to convert e.modifiers to 1/2/4/etc it's throwing errors when 
     * trying to register a new hotkey because it doesn't know what 131072 is
     */

    /*
     * OKAY so I rigged a bunch of stuff here I don't know why GetHashCode returns 
     * 131072 (20000 hex) for shift instead of 0x2 (2 dec) which is the hex code for 
     * shift modifier ???
     * Anyway I'm using the fixed values for detecting if shift/ctrl/alt or any 
     * combination is pressed to not block hotkey from blacklist with use of modifier
     * I'm not very knowledgeable with that sort of stuff so feel free to edit to 
     * make more efficient
     */

    Int32[] Modifiers = { 131072, 65536, 262144 }; //Control, Alt, Shift

    /*
     * BLACKLIST: ctrl+pgdn, ctrl+pgup, caps lock, tab, shift+all numpad (and all 
     * combinations of shift+{X}+num pad), F12, ctrl+shift{e,r,a,j}
     * Q W E R T X D Z A ENT O C S L I H Y K N B U P G M space F8 F1 F 1 2 3 4 5
     */

    Keys[] Blacklist = { Keys.Capital, Keys.CapsLock, Keys.F12, Keys.Tab, Keys.Q, 
                         Keys.W, Keys.E, Keys.R, Keys.T, Keys.X, Keys.D, Keys.Z, 
                         Keys.A, Keys.Enter, Keys.O, Keys.C, Keys.S, Keys.L, Keys.I,
                         Keys.H, Keys.Y, Keys.K, Keys.N, Keys.B, Keys.U, Keys.P, 
                         Keys.G, Keys.M, Keys.Space, Keys.F8, Keys.F1, Keys.F, 
                         Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5 };

    foreach (Keys k in Blacklist)
        if (k == pressedKey)
            if (modifierKeys.GetHashCode() == Modifiers[0] ||
                modifierKeys.GetHashCode() == Modifiers[1] ||
                modifierKeys.GetHashCode() == Modifiers[2] ||
                modifierKeys.GetHashCode() == Modifiers[0] + Modifiers[1] ||
                modifierKeys.GetHashCode() == Modifiers[0] + Modifiers[2] ||
                modifierKeys.GetHashCode() == Modifiers[2] + Modifiers[1])
            {
                //Console.WriteLine("No block"); //yes it finally works!
            }
            else
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                return;
            }

    if (e.KeyCode == Keys.Escape)
        ((TextBox)sender).Parent.Focus();
    else if (pressedKey != Keys.ShiftKey && 
             pressedKey != Keys.Menu && 
             pressedKey != Keys.ControlKey && 
             pressedKey != Keys.None)
             //pressedKey != Keys.None modifierKeys != Keys.None
    {
        Set_Hotkey(_sender_id, e.Modifiers.GetHashCode(), e.KeyCode.GetHashCode());
        ((TextBox)sender).Parent.Focus();
    }
    else
        ((TextBox)sender).Text = new KeysConverter().ConvertToString(e.Modifiers);

    e.Handled = true;
    e.SuppressKeyPress = true;
}

The problem I'm having is that e.Modifiers.GetHashCode() returns 131072(ctrl), 65536(alt), 262144(shift) or any sum of those combinations for multi-modifiers. Any other key I do GetHashCode() on (besides the modifiers) returns the right values and there's no problems.

When the code gets to

private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

it will always fail to register the hotkey with a modifier because it doesn't know what 131072 is it's expecting 2 all the other logic in the code is sound and will function properly if I try and register just (J key) for example

I don't understand why this is happening is there anyway to fix it simpl? such as getting 1,2,4,5,6,7 (proper modifiers that RegisterHotKey is expecting). I have something in mind which is a bit ugly/bulky like

int32 _mod;

if (e.modifier.gethashcode() == 131072)
    _mod = 2;

and so on and so forth with all sums of combinations which is similar to what I did for blacklist checking with modifiers

Also, I'm sorry for really weird commenting I usually don't comment code but I'm pushing this whole program to github so I'm trying to comment more on things I do and this is my least commented void cause I keep revising it so much trying to fix it.

krillgar
  • 12,596
  • 6
  • 50
  • 86
Snowlove
  • 11
  • 3
  • Two things: 1) I reformatted your code so that we don't have to horizontally scroll. Are the indents as you intended? Not having curly braces around `foreach`, `if`, and `else` statements is bad practice, and can make your code harder to read and debug. 2) Why are you using `GetHashCode()` on the items to determine the modifiers? The `Modifiers` property returns the `Keys` enum as well. – krillgar Oct 07 '17 at 20:45
  • @krillgar Sorry I'll start using curlys more often! thanks for the tip. I was trying to get an integer representation of the modifier so I can pass it directly to RegisterHotKey() which I picked up from reading someone elses post about hotkeys and it was working pretty well for all purposes until I got to this point. I'm assuming now that was a bad method to chose. right now I store all virtualkeys and modifiers as integers in variables and config file for saving the user set hotkey so they're ready on load to pass directly to RegisterHotKey() – Snowlove Oct 07 '17 at 21:00
  • There is a problem with `GetHashCode()` though, where there are more combinations for a hash code than there are possibilities for hashes. For example, every integer can be assumed to have a hash code of itself. What then does that mean if you get a hash code for `DateTime` or `string`? You're going to have some overlap. When you're trying to figure out what key it is, go by what key you're told. – krillgar Oct 07 '17 at 21:08

1 Answers1

1

What I went with

Dictionary<Keys, int> _mods = new Dictionary<Keys, int> {
    { Keys.Alt, 1 },
    { Keys.Control, 2 },
    { Keys.Alt ^ Keys.Control, 3 },
    { Keys.Shift, 4 },
    { Keys.Alt ^ Keys.Shift, 5 },
    { Keys.Shift ^ Keys.Control, 6 },
    { Keys.Alt ^ Keys.Shift ^ Keys.Control, 7}
};

usage:

Set_Hotkey(_sender_id, //id
 _mods.ContainsKey(e.Modifiers) ? _mods[e.Modifiers] : 0, //modifier
 e.KeyCode.GetHashCode() //vk
);

works perfectly yay!

Snowlove
  • 11
  • 3