Solved

Simulate Keyboard inputs

Posted on 2001-09-02
19
760 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GIF file bit manipulation for color change 5 107
Error on moodle after upgrade 3 119
bigDiff challenge 17 83
Way to decrease size of apk file 9 40
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

943 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

6 Experts available now in Live!

Get 1:1 Help Now