Detecting if 'Control' or 'Shift' key was down when key pressed
I have been looking into detecting key stuff, it works great, I am trying to figure out if control or shift was pressed, I read in some MSDN documentation (I use Visual C++), that lParam is 0 if ctrl or shift isn't down, or 1 if they are, but how do I tell which one?
I may be wrong, it had a table structure saying stuff like:
0-15 repeat count
16-18 reserved, do not use
19-23 = something
24 = if the right hand shift or control key is down on an extended 101-102 key keyboard.. returns 1 if it is, otherwise returns 0
My code for keys:
Code:
CASE WM_KEYDOWN:
{
if (LOWORD(lParam) == 1)
{
MessageBox(hWnd, _T("Ctrl Or Shift down"), _T("CAP"), 0);
}
int iKey = LOWORD(wParam);
if (iKey == 37) //LEFT ARROW
{
MessageBox(hWnd, _T("LA"), _T("CAP"), 0);
}
else if (iKey == 39) //RIGHT ARROW
{
MessageBox(hWnd, _T("RA"), _T("CAP"), 0);
}
}
I get the message Ctrl Or Shift down, when I hold either of them down, but I don't if I don't hold them down...
Re: Detecting if 'Control' or 'Shift' key was down when key pressed
Your code does not make much sense to me but if you want to check the state of other keys, such as Ctrl, Alt, or Shift while processing WM_KEYUP/DOWN, you can use GetKeyState API function.
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com/
Re: Detecting if 'Control' or 'Shift' key was down when key pressed
Actually it is not a flow. You are interested if Ctrl was down at the time the other key was pressd, not whether it is down or up at the moment you are processing the message.
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com/
Re: Detecting if 'Control' or 'Shift' key was down when key pressed
Hi Boris K K,
I was confused with GetKeyState after I was reading the book. After reading your post, I guess I am still confused
Here is my understanding of GetKeyState:
Let's say we are using GetKeyState to determine if a letter should be displayed as capitalized or not
Let's say the message queue contains ['a']['p']['p']['shift']['l']['e'], so we know the user types 'appLe'
If we hold 'shift' key while the message queue is being processed, we will get 'APPLE' instead of 'appLe'
I believe your statement
Originally Posted by Boris K K
not whether it is down or up at the moment you are processing the message.
is targeting this case I just described.
The lesson that I got out of this is that "it is not a good practice to use GetKeyState to determine if a letter is capitalized or not."
Question:
Could you give me an example of when you will want to use GetKeyState?
Re: Detecting if 'Control' or 'Shift' key was down when key pressed
Originally Posted by kabilius
...Let's say the message queue contains ['a']['p']['p']['shift']['l']['e'], so we know the user types 'appLe'
If we hold 'shift' key while the message queue is being processed, we will get 'APPLE' instead of 'appLe'
I believe your statement
is targeting this case I just described.
The lesson that I got out of this is that "it is not a good practice to use GetKeyState to determine if a letter is capitalized or not."
No, using GetAsyncKeyState (is Shift down right now) may result in incorrect capitalization. GetKeyState (was Shift down when 'l' was pressed; was Shift down when 'e' was pressed, etc.) should be OK.
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com/
Re: Detecting if 'Control' or 'Shift' key was down when key pressed
void CMousepresentView::OnDraw(CDC* pDC)
{
int shiftValue=::GetKeyState(VK_SHIFT);
if(!shiftValue)
pDC->TextOut(0,50,"Shift not pressed");
else
pDC->TextOut(0,50,"Shift pressed");
int ctrlValue=::GetKeyState(VK_CONTROL);
if(!ctrlValue)
pDC->TextOut(0,100,"Ctrl not pressed");
else
pDC->TextOut(0,100,"Ctrl pressed");
}
In this program when I run the program output is
shift pressed
ctrl not pressed
Why "shift pressed" comes even I don't press shift.
Re: Detecting if 'Control' or 'Shift' key was down when key pressed
If this is in your OnDraw routine, you'll need to invalidate your drawing area to get a repaint. Try handling WM_KEYDOWN, and call Invalidate() from it's handler.
Bookmarks