Avoid making the code needlessly complicated. Just do it like this:
RegisterHotKey(_hWnd,
2,
(chkCtrl.Checked ? fsModifiers.Ctrl : 0)
| (chkAlt.Checked ? fsModifiers.Alt : 0)
| (chkShift.Checked ? fsModifiers.Shift : 0)
| (chkWin.Checked ? fsModifiers.Win : 0),
(uint)Keys.S);
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:
enum fsModifiers : uint
{
Alt = 0x0001,
Ctrl = 0x0002,
Shift = 0x0004,
Win = 0x0008,
NoRepeat = 0x4000,
};
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 the RegisterHotKey
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.