Solved

Simulate Keyboard inputs

Posted on 2001-09-02
19
763 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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