Solved

Simulate Keyboard inputs

Posted on 2001-09-02
19
761 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.

856 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