|
-
March 25th, 2003, 02:06 PM
#1
Problem with Retreiving keystates
I am using keybd_event to set the NUMLOCK on / off.
The keyboard's NUMLOCK state before executing is OFF.
Now I use keybd_event and set the NUMLOCK state to ON.
Then , while trying to retrieve the NUMLOCK keystate through GetKeyState() / GetAsyncKeyState() / GetKeyboardState() , the return value is OFF (ie) 0. But the actual key state is ON.
The GetKeyState() is returning the values of the keystate previous to the calls given to keybd_event. It is not considering the keystate changed due to keybd_event. Could any one help me in this regard.
I want the correct keystate to be retreived even after a keybd_event.
My code is this:
//Keyboard NUMLOCK is OFF
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
short s = GetKeyState(VK_NUMLOCK);
BYTE nState1= LOBYTE(s);
BYTE nState2= HIBYTE(s);
//nstate1 is returning 0 instead of 1.
Thank you
Regards
Wajeed
-
March 25th, 2003, 03:18 PM
#2
Code:
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
short s = GetKeyState(VK_NUMLOCK);
if ((s & 1))
TRACE(_T("NumLock off...\n"));
else
TRACE(_T("NumLock on...\n"));
Regards,
Emanuel Vaduva
-
March 26th, 2003, 02:24 AM
#3
This is my code. As per your suggestion that works when i simply toggle the key each and every time. But in the following case , it is a problem.
void CKeystateDlg::OnButton1()
{
if (AIT_SetKeyState(VK_NUMLOCK,1))
AfxMessageBox("true");
}
void CKeystateDlg::OnButton2()
{
// TODO: Add your control notification handler code here
int n = AIT_GetKeyState(VK_NUMLOCK);
if (n==0)
//TRACE(_T("NumLock off...\n"));
AfxMessageBox("off");
else
//TRACE(_T("NumLock on...\n"));
AfxMessageBox("on");
}
void CKeystateDlg::OnButton3()
{
// TODO: Add your control notification handler code here
AIT_SetKeyState(VK_NUMLOCK,1);
int n = AIT_GetKeyState(VK_NUMLOCK);
if (n==0)
//TRACE(_T("NumLock off...\n"));
AfxMessageBox("off");
else
//TRACE(_T("NumLock on...\n"));
AfxMessageBox("on");
}
bool AIT_SetKeyState(int r_nVirtualKey, int r_nKeyState)
{
int l_nRet =0; //Stores the KeyState of the Key Sent
int l_nScanCode =0; //Stores the Scan code of the key event
//Special Keys CAPSLOCK,NUMLOCK and SCROLLLOCK are only considered
if ((r_nVirtualKey==VK_CAPITAL)||(r_nVirtualKey==VK_NUMLOCK)||(r_nVirtualKey==VK_SCROLL) || (r_nVirtualKey == VK_INSERT))
{
if ((r_nKeyState==0) || (r_nKeyState==1))
{
//Get the Key State of the Key Sent
l_nRet=::GetKeyState(r_nVirtualKey);
//If the Key state already matches with that in the Keyboard, then return
if ((l_nRet & 1)==r_nKeyState)
{
return FALSE;
}
else
{
//Retreive the scan code of the virtual key
l_nScanCode=::MapVirtualKey(r_nVirtualKey,0);
//To perform a KeyPress with Key Down event
keybd_event(r_nVirtualKey, l_nScanCode, KEYEVENTF_EXTENDEDKEY | 0, 0 );
//To perform a KeyPress with Key Up event
keybd_event(r_nVirtualKey, l_nScanCode, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
return TRUE;
}
}
}
return FALSE;
}
int AIT_GetKeyState(int r_nVirtualKey)
{
short l_nKeyRet = 0; // To store the key state
if ((r_nVirtualKey==VK_CAPITAL)||(r_nVirtualKey==VK_NUMLOCK)||(r_nVirtualKey==VK_SCROLL) ||(r_nVirtualKey==VK_INSERT))
{
l_nKeyRet=::GetKeyState(r_nVirtualKey);
if (l_nKeyRet & 1)
{
return 0;
}
else
{
return 1;
}
}
}
Thank you
Regards
Wajeed
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|