I don't think that it is possible the way you want to do it. Have a look at the available keycodes. There you see, that e.g. backspace
is 8
and tab
is 9
.
In binary, that would be 1000
and 1001
. Using binary operators, you would use OR |
to "combine" the values, which would result in 1001
.
You would check if a value is set via AND &
, e.g. 1001
& 1000
to see, if the backspace key was pressed. Unfortunately, this would also evaluate to true
if only the tab key was pressed (as its value is 1001
).
That said, you can only use such bitwise comparison techniques, if the different values you want to test are powers of 2 only, i.e. 1, 2, 4, 8, 16, 32 and so on, as this represents in binary 1
, 10
, 100
, 1000
,...
For example if we can have a status
variable and possible statuses would be OPEN = 2
, LIGHT ON = 4
and ALARM ON = 8
.
Assume that it is OPEN
and LIGHT ON
, i.e.
0010
| 0100
-------
0110
Here we can easily check whether the ALARM
is on, be using AND: 0110 & 1000 = 0
. But if we would encode ALARM ON
with 6 = 0110
, we could not check this.
What you could do is, to map the key codes to a some power of 2 value and apply binary operations there. The Wikipedia article about bitmasks might be worth reading.
I hope my explanation was somehow clear.