Click here to Skip to main content
15,880,503 members
Articles / Desktop Programming / Win32
Tip/Trick

Simulate a keystroke in windows

Rate me:
Please Sign up or sign in to vote.
3.79/5 (16 votes)
22 Feb 2011CPOL 40.7K   8   20
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)


Written By
Technical Lead Kotha Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

Comments and Discussions

 
GeneralRe: Extend it to what.. a full article? Pin
Mukit, Ataul23-Aug-11 21:16
Mukit, Ataul23-Aug-11 21:16 
Extend it to what.. a full article?
GeneralRe: I say it because you I think you must extend it before publi... Pin
Caner Korkmaz23-Aug-11 4:40
Caner Korkmaz23-Aug-11 4:40 
GeneralReason for my vote of 1 I agree with the others, copying a c... Pin
AspDotNetDev1-Sep-11 7:58
protectorAspDotNetDev1-Sep-11 7:58 
GeneralReason for my vote of 1 It is copy and paste !!! Pin
Caner Korkmaz23-Aug-11 2:10
Caner Korkmaz23-Aug-11 2:10 
GeneralRe: Just becoz u knew it b4hand, doesn't mean everybody knows it... Pin
Mukit, Ataul23-Aug-11 4:18
Mukit, Ataul23-Aug-11 4:18 
GeneralRe: I agree, all the OP has done is copy some code from somewher... Pin
AspDotNetDev1-Sep-11 7:54
protectorAspDotNetDev1-Sep-11 7:54 
GeneralReason for my vote of 1 nothing new Pin
bomihsc24-Mar-11 13:47
bomihsc24-Mar-11 13:47 
GeneralRe: nothing new to u.. so i guess u r entitled to give a 1... Pin
Mukit, Ataul26-Mar-11 18:57
Mukit, Ataul26-Mar-11 18:57 
GeneralReason for my vote of 3 good Pin
Dabberio22-Feb-11 7:06
Dabberio22-Feb-11 7:06 
GeneralReason for my vote of 5 Good reference. If I used this, I wo... Pin
jeyre22-Feb-11 4:10
jeyre22-Feb-11 4:10 
GeneralReason for my vote of 1 Incomplete and badly researched Pin
wim ton21-Feb-11 22:56
wim ton21-Feb-11 22:56 
GeneralRe: I thought tips was not about extensive research.. just somet... Pin
Mukit, Ataul21-Feb-11 23:28
Mukit, Ataul21-Feb-11 23:28 
General[My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Victor Nijegorodov21-Feb-11 22:31
Victor Nijegorodov21-Feb-11 22:31 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Mukit, Ataul21-Feb-11 23:37
Mukit, Ataul21-Feb-11 23:37 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Victor Nijegorodov21-Feb-11 23:51
Victor Nijegorodov21-Feb-11 23:51 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Mukit, Ataul21-Feb-11 23:53
Mukit, Ataul21-Feb-11 23:53 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Victor Nijegorodov22-Feb-11 0:04
Victor Nijegorodov22-Feb-11 0:04 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Mukit, Ataul22-Feb-11 0:22
Mukit, Ataul22-Feb-11 0:22 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
PrafullaVedante2-Mar-11 20:00
PrafullaVedante2-Mar-11 20:00 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Mukit, Ataul4-Mar-11 20:14
Mukit, Ataul4-Mar-11 20:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.