Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

Simulate a keystroke in windows

3.79/5 (16 votes)
22 Feb 2011CPOL 43.1K  
This tip shows how to simulate a key stroke in windows environment
MIDL
void SetNumLock( BOOL bState )
{
   BYTE keyState[256];

   GetKeyboardState((LPBYTE)&keyState);
   if( (bState && !(keyState[VK_NUMLOCK] & 1)) ||
       (!bState && (keyState[VK_NUMLOCK] & 1)) )
   {
   // Simulate a key press
      keybd_event( VK_NUMLOCK,
                   0x45,
                   KEYEVENTF_EXTENDEDKEY | 0,
                   0 );

   // Simulate a key release
      keybd_event( VK_NUMLOCK,
                   0x45,
                   KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                   0);
   }
}

void main()
{
   SetNumLock( TRUE );
}


Reference MSDN (Thanx to Victor Nijegorodov)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)