I have multiple checkboxes. CTRL[], ALT[], SHIFT[]. Is it possible to add modifier if checkobx is checked?
RegisterHotKey(_hWnd, 2, (uint)fsModifiers.None, (uint)LSettings.Key_ON);
Example of what i want to achieve: If checkbox CTRL[X] and ALT[X] are checked:
RegisterHotKey(_hWnd, 2, (uint)fsModifiers.Control | (uint)fsModifiers.Alt, (uint)Keys.S);
I know i can use 7 IFs, but this it would be very messy, since user will be able to check if he wants to use ctrl and or alt and or shift and or key,. I even tried with some arrays, but no idea how to solve this.
Avoid making the code needlessly complicated. Just do it like this:
where
chkCtrl
, etc. are the names of your checkbox controls.I don't know why you're casting each
fsModifiers
value to uint. I have removed those casts from my code. If you want to ensure that you pass a uint value, then just declare the enumeration that way:Yes, this way you do effectively have 4 "IF" statements. The conditional operators will likely compile down to exactly the same IL as if you had written "IF" statements. But, in this case, they are easier to read.
There's not going to be any real performance advantage to finding some complicated way of rewriting this logic with bit arrays. The bottleneck will not be the logic to compute the parameters to pass to the
RegisterHotKey
function, it will be the actual call to theRegisterHotKey
function. There is no way for either the compiler or the JIT compiler to optimize this, you're P/Invoking an external function located in a system DLL. That's slow (relatively speaking, of course; it is still not a performance problem in an application).And personally, I think there would be a readability (and therefore maintenance) cost to making the logic more complicated. With the code above, anyone with basic programming knowledge can figure out what is happening.