Board index » delphi » Simulate keyboard and mouse events?

Simulate keyboard and mouse events?

Is it possible to simulate keyboard and mouse actions from within a Delphi
program?

I have two separate situations where I need to do this.

First, in a menu, I want the program to act as if the user had pressed the
down-arrow key.

Second, in a richedit, I want the program to act as if the user had clicked
the scroll-down button of the vertical scrollbar.  (Clicking the button
seems to do a lot of housekeeping that a call to the ScrollBy method does
not.)

Is this possible?

Thanks in advance,
Ciarn Duibhn.

 

Re:Simulate keyboard and mouse events?


"Ciarn Duibhn" <cia...@oduibhin.freeserve.co.uk> wrote in message
news:39e8571d_2@dnews...

Quote
> Is it possible to simulate keyboard and mouse actions from within a Delphi
> program?

> I have two separate situations where I need to do this.

> First, in a menu, I want the program to act as if the user had pressed the
> down-arrow key.

> Second, in a richedit, I want the program to act as if the user had
clicked
> the scroll-down button of the vertical scrollbar.  (Clicking the button
> seems to do a lot of housekeeping that a call to the ScrollBy method does
> not.)

You can post a WM_KEYDOWN message to simulate a keypress. Use the
Application.MainForm.Handle in the parameters. There are similar messages
for WM_MOUSEDOWN I believe, but you can find these in the help files.

HTH
Woody

Re:Simulate keyboard and mouse events?


For keyboard events look the API function keybd_event. When using it don't
forget always to generate the keydown and keyup, I had problems before when
generating only keydowns, it created some strange interactions with Windows
Explorer where explorer remained locked. Specifically it was with the escape
key.

Sebastian
"Ciarn Duibhn" <cia...@oduibhin.freeserve.co.uk> escribi en el mensaje
news:39e8571d_2@dnews...

Quote
> Is it possible to simulate keyboard and mouse actions from within a Delphi
> program?

> I have two separate situations where I need to do this.

> First, in a menu, I want the program to act as if the user had pressed the
> down-arrow key.

> Second, in a richedit, I want the program to act as if the user had
clicked
> the scroll-down button of the vertical scrollbar.  (Clicking the button
> seems to do a lot of housekeeping that a call to the ScrollBy method does
> not.)

> Is this possible?

> Thanks in advance,
> Ciarn Duibhn.

Re:Simulate keyboard and mouse events?


Quote
In article <39e8571d_2@dnews>, Ciarn Duibhn wrote:
> Is it possible to simulate keyboard and mouse actions from within a Delphi
> program?

Yes.

Quote
> I have two separate situations where I need to do this.

> First, in a menu, I want the program to act as if the user had pressed the
> down-arrow key.

In this case you need to use the keybd_event function (API, win32.hlp). It
puts key events into the active processes message loop exactly as if the user
hacked away on the keyboard.

 keybd_event( VK_F10, MapVirtualkey( VK_F10, 0 ), 0, 0);
 keybd_event( VK_F10, MapVirtualkey( VK_F10, 0 ), KEYEVENTF_KEYUP, 0);

This will activate the main menu bar.

 keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN,0), 0, 0);
 keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN,0), KEYEVENTF_KEYUP, 0);

This will open the selected submenu.

If you go to http://www.deja.com, http://www.mers.com/searchsite.html or
http://www.tamaracka.com/search.htm and search the newsgroups for PostKeyEx32
and SendText you will find some useful wrappers around keybd_event that are a
bit easier to use.

Quote
> Second, in a richedit, I want the program to act as if the user had clicked
> the scroll-down button of the vertical scrollbar.  (Clicking the button
> seems to do a lot of housekeeping that a call to the ScrollBy method does
> not.)

This i would solve that by sending WM_VSCROLL messages to the richedit. This
is what the scrollbar does when the user clicks on the down scroll arrow:

  sendmessage( richedit.handle, WM_VSCROLL, SB_LINEDOWN, 0 );
  sendmessage( richedit.handle, WM_VSCROLL, SB_ENDSCROLL, 0 );

Sending key messages for the down arrow key to the control would not do quite
the same thing, since it moves the caret, which scrolling by the scrollbar
does not do.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Re:Simulate keyboard and mouse events?


Excellent!  I can now, e.g. on a form's OnShow event, simulate a VK_F10,
then the accelerator character for a main menu item, and then one or more
VK_DOWNs, to have the program navigate straight to a main menu item and
specific item of its submenu.  VK_DOWNs on their own also work fine in the
OnPopup event of a popup menu.
However, I can't get this to work in the OnClick procedure for a main menu
item.  The OnClick procedure fires whenever the submenu is displayed,
whether by depressing (or depressing and releasing) the mouse over the main
menu item, or by doing the same over another main menu item (with a submenu)
and dragging the mouse sideways over the desired main menu item.  I hoped
that VK_DOWNs here would navigate down the submenu, but nothing happens.

I have still to take up the advice about scrolling, but I'd like to solve
this problem first.  Can anyone help further?

Ciarn Duibhn.

Peter Below (TeamB) <100113.1...@compuXXserve.com> wrote:

Quote
> ...
> > First, in a menu, I want the program to act as if the user had pressed
> > the down-arrow key.

> In this case you need to use the keybd_event function (API, win32.hlp). It
> puts key events into the active processes message loop exactly as if the
> user hacked away on the keyboard.

>  keybd_event( VK_F10, MapVirtualkey( VK_F10, 0 ), 0, 0);
>  keybd_event( VK_F10, MapVirtualkey( VK_F10, 0 ), KEYEVENTF_KEYUP, 0);

> This will activate the main menu bar.

>  keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN,0), 0, 0);
>  keybd_event( VK_DOWN, MapVirtualKey( VK_DOWN,0), KEYEVENTF_KEYUP, 0);

> This will open the selected submenu.

> If you go to http://www.deja.com, http://www.mers.com/searchsite.html or
> http://www.tamaracka.com/search.htm and search the newsgroups for
> PostKeyEx32 and SendText you will find some useful wrappers around
> keybd_event that are a bit easier to use.

> > Second, in a richedit, I want the program to act as if the user had
> > clicked the scroll-down button of the vertical scrollbar.  (Clicking
> > the button seems to do a lot of housekeeping that a call to the ScrollBy
> > method does not.)

> This i would solve that by sending WM_VSCROLL messages to the richedit.
> This is what the scrollbar does when the user clicks on the down scroll
arrow:

>   sendmessage( richedit.handle, WM_VSCROLL, SB_LINEDOWN, 0 );
>   sendmessage( richedit.handle, WM_VSCROLL, SB_ENDSCROLL, 0 );

> Sending key messages for the down arrow key to the control would not do
> quite the same thing, since it moves the caret, which scrolling by the
scrollbar
> does not do.

Re:Simulate keyboard and mouse events?


Quote
In article <39eb7e95_2@dnews>, Ciarn Duibhn wrote:
> However, I can't get this to work in the OnClick procedure for a main menu
> item.  The OnClick procedure fires whenever the submenu is displayed,
> whether by depressing (or depressing and releasing) the mouse over the main
> menu item, or by doing the same over another main menu item (with a submenu)
> and dragging the mouse sideways over the desired main menu item.  I hoped
> that VK_DOWNs here would navigate down the submenu, but nothing happens.

If the menu won't eat the key events you feed it you can do nothing about
that. Windows handles key and mouse processing while a menu is active, perhaps
it ignores most keyboard input while the mouse button is down (otherwise the
mouse position may be disconnected from the active menu item, which would
certainly confuse the user and probably also the menu handling code in
Windows).

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Re:Simulate keyboard and mouse events?


That interesting thought led me to wonder what if I activated the submenu by
using an accelerator key instead of the mouse - and  it works!  So VK_DOWNs
to place the "focus" on a  selected item of a submenu can be successfully
simulated in the OnClick procedure when the submenu is thrown up using the
accelerator key, but not when it is thrown up using the mouse.
I still think it's a pity the focus can't be placed simply by setting a
property, though.

Ciarn Duibhn.

Peter Below (TeamB) <100113.1...@compuXXserve.com> wrote in message
news:VA.00006005.00fbeb40@antispam.compuserve.com...

Quote
> In article <39eb7e95_2@dnews>, Ciarn Duibhn wrote:
> > However, I can't get this to work in the OnClick procedure for a main
menu
> > item.  The OnClick procedure fires whenever the submenu is displayed,
> > whether by depressing (or depressing and releasing) the mouse over the
main
> > menu item, or by doing the same over another main menu item (with a
submenu)
> > and dragging the mouse sideways over the desired main menu item.  I
hoped
> > that VK_DOWNs here would navigate down the submenu, but nothing happens.

> If the menu won't eat the key events you feed it you can do nothing about
> that. Windows handles key and mouse processing while a menu is active,
perhaps
> it ignores most keyboard input while the mouse button is down (otherwise
the
> mouse position may be disconnected from the active menu item, which would
> certainly confuse the user and probably also the menu handling code in
> Windows).

> Peter Below (TeamB)  100113.1...@compuserve.com)
> No e-mail responses, please, unless explicitly requested!

Other Threads