What does the 0x80 code mean when referring to keyboard controls - c++

what does the 0x80 code mean when referring to the keyboard controls in C++ Windows environment?
For example,
if(GetKeyState('K') & 0x80) {
//do something
}
Thanks everyone!

Update
A flurry of downvotes propelled me into investigating this further. Here's how the return values (in hex) of GetKeyState works. I don't quite get the toggle property of a key like k but I'm assuming there's some default state it toggles from.
0 Default State, key up
ff80 Default state, key down
1 Toggled, key up
ff81 Toggled, key down
So 0xff80 is added whenever the high-order bit needs to be set and the low-order bit makes sense. So now we know why the 0x80 approach works --- since the high-order bit of the lower byte is set as well!
Old Answer
GetKeyState returns a SHORT where if the high-order bit is 1 it means the key is up. The bitwise AND operation with 0x80 just checks if that bit is 1 since in binary 0x80 is 10000000.
Therefore the statement GetKeyState('K') & 0x80 would return 0x80 if the high-order bit of the value returned by GetKeyState('K') is 1 and 0 if the high-order bit is 0.

The MSDN documentation of the function states:
If the high-order bit is 1, the key is
down; otherwise, it is up.
bit-wise and with 0x80 gives you the high order bit, the if checks if the result is zero or non-zero and in essence checks the value of that bit.
This check however looks like a mistake since GetKeyState() returns a SHORT and to check the high order bit of a short you need to bit-wise and with 0x8000.
So I suggest you check the return value with a debugger and verify how this works in reality.

I think you mean 0x8000, not 0x80. If that is the case, you should consult the documentation (http://msdn.microsoft.com/en-us/library/ms646301(VS.85).aspx) which has the following to say on the return value of GetKeyState:-
The return value specifies the status of the specified virtual key, as follows:
•If the high-order bit is 1, the key is down; otherwise, it is up.
•If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
0x80 doesn't mean anything as far as I know though

According to the documentation
The return value specifies the status of the specified virtual key:
If the high-order bit is 1, the key is down; otherwise, it is up.
If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
Perhaps with a non-toggleable key (such a 'K'), the low-order (ambiguous term - perhaps they mean 0x0080 ?) and high-order (0x8000) bits do the same thing?

Related

how to flush or clear GetAsyncKeyState's buffer

i'm using GetAsyncKeyState in an MFC application to check if Esc button is pressed,
but when i press on Esc button from dialog and use GetAsyncKeyState in a different dialog it returns nonzero because it's exists in the message queue.
how can i clear or flush GetAsyncKeyState's buffer or delete this message from message queue?
Thanks in advance.
The direct answer to your question would be just calling it a second time, discarding the value from the first time.
But I guess that what you really want to know is how to read the current status of the key, regardless of when you last checked. Since you wrote "returns nonzero" I believe you are not using it correctly.
You need to check for the bit with value 0x8000 because this one indicates whether it's pressed right now. The bit with the value 1 is the one which is set if the key was pressed since the last check, and that's the one tripping you over, so just ignore it and directly test for the bit with value 0x8000.
Example code:
if(GetKeyState(VK_RETURN) & 0x8000) yayReturnIsPressed();
Checking if(GetKeyState(VK_RETURN)) or if(GetKeyState(VK_RETURN) != 0 will not do what you want because it will be fulfilled if any of the bits in the return value are set.
In GetAsyncKeyState documentation you can read:
If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.
(emphasis mine)
so to check current state of ESC button you should only check most significant bit:
bool isEscPressed = GetAsyncKeyState(VK_ESCAPE) & 0x8000;
if you check state like that: if (GetAsyncKeyState(VK_ESCAPE)) {} then it will enter if statement even if ESC is not currently pressed.

Can't find a list of hex key when using modifers (shift, alt, ctrl) C++

RegisterHotKey(0, ZERO_KEYID, 0, 0x60) // Registers as numpad 0
Ok I am trying to set up hotkeys and As you can see above I need the hex value for the keyboard. I can find all the values without modifiers but how can I find when using modifiers such as shift, ctrl, alt?
A simple google request brings you Microsoft's website for RegisterHotKey and there you find it:
[https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx][1]
The third parameter lets you specify the modifier(s) like:
RegisterHotKey (0, ZERO_KEYID, MOD_CONTROL|MODSHIFT, 0x60);
As far as I know there is no way of detecting when two arbitrary keys (like '1' and '2') are being pressed simultaneously.
Just use the MSDN documentation on RegisterHotKey.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx

Realization of Truth table in C

I want to set various clock sources in a function as per the truth table below. Basically I want to write to the TCCR0B register(Atmega328) according to the parameter I pass to setClockSource function. The image of the table and registers is given below.
I am not able to makeout how best it can be done. I thought of using enum for various modes as below.
enum CLOCK_SOURCE{
NO_CLOCK_SOURCE=0x0;
NO_PRESCALING=0x01;
CLK_8=0x02;
// and so on
}
But the problem is in setClockSource() function, how should I write to TCCR0B register without affecting bits 3-7? Shall I first clear last 3 bits and then OR TIMER_MODE values with TCCR0B? Without clearing, I may not guarantee the correct values for last 3 bits I guess. What is the efficient way?
void setClockSource (enum CLOCK_SOURCE clockSource)
{
TCCR0B&=0xF8; // First Clear last 3 bits
TCCR0B|=clockSource;
}
Do we have library functions available to set clock source? I am using Atmega studio
Do it like this:
void setClockSource (CLOCK_SOURCE clockSource)
{
TCCR0B = TCCR0B & 0xF8 | clockSource;
}
Thus you will keep high bits and set lower bits.

X11 Unicode KeyEvent Issues

The program I'm working on a virtual keyboard of sorts that requires unicode. Using the code I've received from http://goo.gl/pv9ht and it works for normal (ASCII) keysyms when converting to a keycode, but XKeysymToKeycode() returns 0 on a keysym like XK_agrave (include/X11/keysymdefs.h).
I'm also not quite sure how to do the same with capitals. When I try the same with XK_A (that's capital 'A'), it returns the same keycode as 'a'. This does make sense since they are the same keycode (along with a bunch of other characters) according to the output 'xmodmap -pke'. But how do I make it send (XSendKeyEvent) the capital form of the keycode?
Help would be much appreciated.
You send with a modifier key (shift in this case)
So you set XKeyEvent->state |= ShiftMask
The state member is set to indicate the logical state of the pointer buttons and modifier keys just prior to the event, which is the bitwise inclusive OR of one or more of the button or modifier key masks: Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask, ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, and Mod5Mask.
Source: http://linux.die.net/man/3/xkeyevent

what the snippet of code does

I would like to know what the snippet of code does..
Drive[0] = 'A';
Drive[1] = ':';
Drive[2] = '\\';
Drive[3] = 0;
DriveMask = GetLogicalDrives();
for( anIndex = 0; anIndex < 26;
anIndex++ )
{
if( DriveMask & 1 )
{
Drive[0] = 'A' + anIndex;
DriveMask >>= 1;
}
}
Please let me know your answer.
Thank you for your time to read my post.
It checks if the lowest bit is set i.e. if there is an A drive. See GetLogicalDrives
It's enumerating all the possible attached drives between A:\ and Z:\ and checking to see whether they're removable (eg CD, floppy).
It loops 26 times, and each time
DriveMask >>= 1;
causes the bitmask to be shifted right by 1 bit, so that each logical drive can be tested for via the
if( DriveMask & 1 )
in succession.
GetDriveType() requires a drive path, so the label is constructed by adding the loop count to the letter A (so A, B, C, D, ..., Z) and leaving the previously-initialized :\ part in-place.
In C++ the & is a bitwise and.
So take the value Drives and do a bitwise with 0x00000001. The result should be 1 if the number is odd (only way to have an odd number is with the least significant bit is 1). Since 0 anded with 1 = 0, it basically zeroes out all the values except for the least significant bit. If that bit is 1, then the result is 1 and evaluates to true.
Otherwise it's 0, and you don't hit the if.
It checks if the number is odd.
& is a bit-wise AND comparison.
0101 (5)
& 0001 (1)
= 0001 (1 -- true)
1110 (14)
& 0001 (1)
= 0000 (0 -- false)
In this case, GetLogicalDrives returns a number whose bits indicate the presence of certain drives. The least significant bit (20, 1) indicates the A drive.
The expression Drives & 1 is testing to see that the result of a logical and between Drives and 0x00000001 is non-zero. Thus it is checking to see if Drives is odd.
actually api returns reply in binary format :- here what MSDN says about it
"
If the function succeeds, the return value is a bitmask representing the currently available disk drives. Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.
"
means
if( Drives & 1 ) // i dont understand this if condition here that what it checks ? {
}
Condition checking for digit drive presense.
The GetLogicalDrives function returns the set of logical drives where each drive is encoded as a bit (a binary digit, can be either 0 or 1). The drive labels start at "A" in bit 0 (the least significant bit). The bit is 1 if the drive is present, else it's 0. The & in the above code is a logical-AND operation to test bit 0. Essentially this code checks if the system has an "A:\" drive.
This piece of code does not do absolutely anything in the common understanding of the word do. This code contains only non-modifying query-type operations with no side effects, i.e. it makes some queries and verifies some conditions, but it doesn't make any actions based on the results of these conditions.
In other words, if this code was fed into some hypothetical super-optimizing compiler, which also knows the Windows API, that compiler would simply throw out (optimize away) the entire code, since it doesn't do anything.
Apparently, the code you provided is fake - it is not the whole code. Without the whole thing, it is impossible to say what it was supposed to do. However, if we guess that some useful functionality was supposed to be present between the {} in the following if
if( GetDriveType( Drive ) == DRIVE_REMOVABLE )
{
// Actually DO something here
}
then we can make an educated guess about what it was supposed to do. This code iterates though all possible single-letter drive designations in a Windows system. It checks whether a logical drive designated by that letter is present in the system. And if the drive is present, it checks whether this drive works with removable media. And, finally, if it is true, then it does something useful that you are not showing us. I don't know what it was. Nobody does.