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.