Solved

Simulate Keyboard inputs

Posted on 2001-09-02
19
759 Views
Last Modified: 2013-11-20
Hi, i want to know how can i simulate keyboard inputs from the software?
I have a digital camera software that can be used to capture image from the camera when you press the Hotkey "Shift-F12". Since i want to write my software to capture the image, then process it ... so i need to simulate the "Shift-F12" command using my own VC++ software to trigger the digital camera..
can anyone tell me how to do that??
hope i explained my question clearly :)
0
Comment
Question by:rw8
  • 9
  • 4
  • 3
  • +1
19 Comments
 
LVL 2

Expert Comment

by:xu2000
ID: 6449609
Hi,
keybd_event( VK_SHIFT, 0, 0, 0);
keybd_event( VK_F12, 0, 0, 0);
keybd_event( VK_F12, 0, KEYEVENTF_KEYUP, 0);
keybd_event( VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);

Note, the window need be focused.

Xu

0
 
LVL 2

Expert Comment

by:xu2000
ID: 6449620
or SendMessage (PostMessage) WM_KEYUP WM_KEYDOWN message.
0
 
LVL 2

Expert Comment

by:vbk_bgm
ID: 6450623
If it is a hotkey registered by the digital camera software using RegisterHotkey,
you can PostMessage the WM_HOTKEY message with the parameters
idHotKey : 0
fuModifiers: MOD_SHIFT
uVirtKey: VK_F12

You can get more info from MSDN.
0
 
LVL 4

Expert Comment

by:aponcealbuerne
ID: 6451791
look for the "SendInput" that function does the work...

Hope helps
0
 

Author Comment

by:rw8
ID: 6451957
I will be using Windows2000 and according to MSDN.. i should use "SendInput" instead of keybd_event..
but can aponcealbuerne (or anyone else) give me an example of how to use SendInput to simulate the "Shift-f12" input?
0
 

Author Comment

by:rw8
ID: 6452019
btw, i've added this to my software:
PostMessage(WM_HOTKEY,0,MOD_SHIFT + VK_F12);    
but it seems not working...
did i do something wrong???
0
 

Author Comment

by:rw8
ID: 6452054
btw, i've added this to my software:
PostMessage(WM_HOTKEY,0,MOD_SHIFT + VK_F12);    
but it seems not working...
did i do something wrong???
0
 
LVL 2

Expert Comment

by:vbk_bgm
ID: 6452176
LPARAM lParam = MAKELPARAM(MOD_SHIFT,VK_F12);
PostMessage(WM_HOTKEY,0,lParam);
if the above does not work, then
::PostMessage(HWND_BROADCAST,WM_HOTKEY,0,lParam);

I guess the digital camera software is a different application to which you are sending the keyboard events. Then I think the SendInput functions work only for the same application.
0
 

Author Comment

by:rw8
ID: 6452271
Seems doesn't work....
when i use PostMessage(WM_HOTKEY,0,lParam);
no response..
when i use ::PostMessage(HWND_BROADCAST,WM_HOTKEY,0,lParam);
the Windows "Run.." dialog box (that can be called by pressing windows-R) pops up... and even if i change the
LPARAM lParam = MAKELPARAM(MOD_SHIFT,VK_F12);
to
LPARAM lParam = MAKELPARAM(MOD_ALT,VK_F4);
it still do the same thing..
what did i do wrong??

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

by:vbk_bgm
ID: 6452323
try,
::PostMessage(HWND_BROADCAST,WM_KEYDOWN,VK_SHIFT,0);
::PostMessage(HWND_BROADCAST,WM_KEYDOWN,VK_F12,0);
::PostMessage(HWND_BROADCAST,WM_KEYUP,VK_F12,0);
::PostMessage(HWND_BROADCAST,WM_KEYUP,VK_SHIFT,0);
0
 

Author Comment

by:rw8
ID: 6452350
Doesn't work :(
i've tried
(LPARAM lParam = MAKELPARAM(MOD_SHIFT,VK_F12);
::PostMessage(HWND_BROADCAST,WM_KEYDOWN,0,lParam);
::PostMessage(HWND_BROADCAST,WM_KEYUP,0,lParam);

and also tried

HWND hWnd=::FindWindow(NULL,"RemoteCapture");
if(hWnd!=NULL){
     LPARAM lParam = MAKELPARAM(MOD_SHIFT,VK_F12);
     ::PostMessage(hWnd,WM_KEYDOWN,0,lParam);
     ::PostMessage(hWnd,WM_KEYUP,0,lParam);
     ::PostMessage(hWnd,WM_HOTKEY,0,lParam);
}


None working :(
0
 
LVL 2

Expert Comment

by:xu2000
ID: 6452766
Have you registered the hotkey? Does RegisterHotKey succeed?

Xu
0
 
LVL 4

Expert Comment

by:aponcealbuerne
ID: 6453871



you'll need to include #include "winuser.h"
also to define

#define _WIN32_WINNT 0X0401

EnumWindows(
     MyFunc,  // pointer to callback function
     0L
     );

     UINT cInputs = 1;
     INPUT i;
     i.type = INPUT_KEYBOARD;
        i.ki.dwExtraInfo = 0;
     i.ki.dwFlags = 0;
     i.ki.time = 0;
     i.ki.wScan = 0x2A;
     i.ki.wVk = VK_SHIFT;
     SendInput(1,&i,sizeof(INPUT));    
        i.ki.wVk = VK_F12;
        SendInput(1,&i,sizeof(INPUT));
 
where MyFunc is defined something like this:




BOOL CALLBACK MiFunc(HWND hwnd, LPARAM lParam )
{
     char strWindow[50];
     GetWindowText(hwnd,strWindow,50);
     if(!strcmp(strWindow, "Your app name")) //Here compare the name of the app window that you want to send the keyboard input.
     {
          SetForegroundWindow(hwnd);
          ShowWindow(hwnd, SW_SHOW);
     
          return FALSE;
     }
     return TRUE;
}
 

Hope helps
0
 

Author Comment

by:rw8
ID: 6454811
XU2000: why do i need to register the hotkey.. and how should i do that? the hotkey "shift-F12" is from the software that comes with the digital camera.. so do i still need to register it?

aponcealbuerne: Trying your code now ....
0
 

Author Comment

by:rw8
ID: 6454908
aponcealbuerne: Why the compiler can't recognise "INPUT" ? i've already include "winuser.h"...
0
 
LVL 2

Expert Comment

by:xu2000
ID: 6455233
Then the camera app and your app is NOT same one, do you sure the camera app register hotkey by RegisterHotKey? You can't use SendInput because not same app.

Xu
0
 

Author Comment

by:rw8
ID: 6455375
The code aponcealbuerne listed above is working! Thanx..
but then i've the manually press the "SHIFT" key after calling that to make everything back to normal..
otherwise.. it's just like the SHIFT key has been pressed and holded forever.. why and how to solve it?
XU2000:Yes.. the camera app and your app are NOT same one
0
 
LVL 4

Accepted Solution

by:
aponcealbuerne earned 100 total points
ID: 6461026
ok you have to set

i.ki.dwFlags = KEYEVENTF_KEYUP;

and SendInput again, in order to release the key....
0
 

Author Comment

by:rw8
ID: 6469576
Thanks very much.. and thanx to other ppl who gave me help
(sorry about the late reply)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Powershell based monitoring system 2 127
Handling string inputs in C/Linux 23 168
sum28 challenge 31 96
EvenOdd challenge 10 83
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now