How to force a key press/mouse click event?

K

Krupa

Hi there,

Does anybody know how to throw key press and mouse click events
programmatically without actuaaly pressing a key or clicking the mouse?

I am working on a c# application on CF 2.0 for WinCE 5.0 devices.

Thanks,
Krupa
 
K

Krupa

IndridCold said:
you can use the PInvoke function(s) SendInput() or keyb_event()


I tried P/Invoking SendInput() function in User32.dll.But I get a
missing method exception saying it can't find the PInvoke DLL
User32.dll. Do I need to use any other dll or copy this dll to some
place?

BTW, I am working on a windows application on CF 2.0 for WinCE 5.0
devices.

Regards,
Krupa
 
P

Paul G. Tobey [eMVP]

There is no User32.dll on Windows CE. Coredll.dll is the most-likely place
for SendInput...

Paul T.
 
K

Krupa

Thanks Paul and IndridCold.

I am trying tp use the keybd_event function now. Can you tell me where
the dwFlags and dwExtraInfo are defined and what values of it should I
use?

Thanks,
Krupa
 
P

Paul G. Tobey [eMVP]

The right way to handle this is to install the C++ SDK for your target
device and look at the help page for the call. Once you see that, ah, I
need to pass KEYEVENTF_KEYUP, you can easily look that up in the header
files from the SDK and find the value.

Paul T.
 
K

Krupa

Thanks again Paul! I am moving forward.

I am trying to bring my Flash movie that is launched on a browser to
focus by synthesizing a 'tab' followed by 'enter' key press events. But
I get an error when I do this. Am I going the right way? Please find my
code below.

[DllImport("coredll.dll", SetLastError = true)]
public static extern void keybd_event(byte bVk, byte bScan, uint
dwFlags, uint dwExtraInfo);

const byte VK_TAB = 0x09;
const byte VK_RETURN = 0x0D;
const uint KEYEVENTF_KEYUP = 0x02;

webBrowser1.Navigate(first);

keybd_event(VK_TAB, 0, 0, 0);
Thread.Sleep(100);
keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0);
Thread.Sleep(100);
keybd_event(VK_RETURN, 0, 0, 0);
Thread.Sleep(100);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
if (Marshal.GetLastWin32Error() != 0)
{
MessageBox.Show(Marshal.GetLastWin32Error().ToString());
}

I get an error code of -2147483643 which seems weird.

-Krupa
 
P

Paul G. Tobey [eMVP]

There's no reason to believe that keybd_event() is setting the Win32 error.
Does the code do what you want it to do? That's the only 'success' test.
keybd_event() doesn't return an error indication.

Paul T.

Krupa said:
Thanks again Paul! I am moving forward.

I am trying to bring my Flash movie that is launched on a browser to
focus by synthesizing a 'tab' followed by 'enter' key press events. But
I get an error when I do this. Am I going the right way? Please find my
code below.

[DllImport("coredll.dll", SetLastError = true)]
public static extern void keybd_event(byte bVk, byte bScan, uint
dwFlags, uint dwExtraInfo);

const byte VK_TAB = 0x09;
const byte VK_RETURN = 0x0D;
const uint KEYEVENTF_KEYUP = 0x02;

webBrowser1.Navigate(first);

keybd_event(VK_TAB, 0, 0, 0);
Thread.Sleep(100);
keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0);
Thread.Sleep(100);
keybd_event(VK_RETURN, 0, 0, 0);
Thread.Sleep(100);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
if (Marshal.GetLastWin32Error() != 0)
{
MessageBox.Show(Marshal.GetLastWin32Error().ToString());
}

I get an error code of -2147483643 which seems weird.

-Krupa

The right way to handle this is to install the C++ SDK for your target
device and look at the help page for the call. Once you see that, ah, I
need to pass KEYEVENTF_KEYUP, you can easily look that up in the header
files from the SDK and find the value.

Paul T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top