Click here to Skip to main content
12,630,940 members (42,350 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

27.6K views
7 bookmarked
Posted

Simulate a keystroke in windows

, 22 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip shows how to simulate a key stroke in windows environment
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)

Share

About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

You may also be interested in...

Comments and Discussions

 
GeneralRe: Extend it to what.. a full article? Pin
Mukit, Ataul23-Aug-11 22:16
memberMukit, Ataul23-Aug-11 22:16 
GeneralRe: I say it because you I think you must extend it before publi... Pin
Caner Korkmaz23-Aug-11 5:40
memberCaner Korkmaz23-Aug-11 5:40 
GeneralReason for my vote of 1 I agree with the others, copying a c... Pin
AspDotNetDev1-Sep-11 8:58
protectorAspDotNetDev1-Sep-11 8:58 
Reason for my vote of 1
I agree with the others, copying a code snippet from MSDN and pasting it as a tip here is of no value. Eventually, we'll just end up with a copy of MSDN in another location.
GeneralReason for my vote of 1 It is copy and paste !!! Pin
Caner Korkmaz23-Aug-11 3:10
memberCaner Korkmaz23-Aug-11 3:10 
GeneralRe: Just becoz u knew it b4hand, doesn't mean everybody knows it... Pin
Mukit, Ataul23-Aug-11 5:18
memberMukit, Ataul23-Aug-11 5:18 
GeneralRe: I agree, all the OP has done is copy some code from somewher... Pin
AspDotNetDev1-Sep-11 8:54
protectorAspDotNetDev1-Sep-11 8:54 
GeneralReason for my vote of 1 nothing new Pin
bomihsc24-Mar-11 14:47
memberbomihsc24-Mar-11 14:47 
GeneralRe: nothing new to u.. so i guess u r entitled to give a 1... Pin
Mukit, Ataul26-Mar-11 19:57
memberMukit, Ataul26-Mar-11 19:57 
GeneralReason for my vote of 3 good Pin
Diogny Aguilera22-Feb-11 8:06
memberDiogny Aguilera22-Feb-11 8:06 
GeneralReason for my vote of 5 Good reference. If I used this, I wo... Pin
jonathanbeyre22-Feb-11 5:10
memberjonathanbeyre22-Feb-11 5:10 
GeneralReason for my vote of 1 Incomplete and badly researched Pin
wim ton21-Feb-11 23:56
memberwim ton21-Feb-11 23:56 
GeneralRe: I thought tips was not about extensive research.. just somet... Pin
Mukit, Ataul22-Feb-11 0:28
memberMukit, Ataul22-Feb-11 0:28 
General[My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Victor Nijegorodov21-Feb-11 23:31
memberVictor Nijegorodov21-Feb-11 23:31 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Mukit, Ataul22-Feb-11 0:37
memberMukit, Ataul22-Feb-11 0:37 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Victor Nijegorodov22-Feb-11 0:51
memberVictor Nijegorodov22-Feb-11 0:51 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Mukit, Ataul22-Feb-11 0:53
memberMukit, Ataul22-Feb-11 0:53 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Victor Nijegorodov22-Feb-11 1:04
memberVictor Nijegorodov22-Feb-11 1:04 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Mukit, Ataul22-Feb-11 1:22
memberMukit, Ataul22-Feb-11 1:22 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
PrafullaVedante2-Mar-11 21:00
memberPrafullaVedante2-Mar-11 21:00 
GeneralRe: [My vote of 2] Is it a plagiarism or you just forgot to provide a link_ Pin
Mukit, Ataul4-Mar-11 21:14
memberMukit, Ataul4-Mar-11 21: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 22 Feb 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid