locked
Using Keyboard and Mouse Hooks RRS feed

  • Question

  • Hi all,

              I am using win-32 hooks to implement macro recording and playback functionality for our application. This is not general purpose utility to use across application but a feature within the application. I launch record/playback palette as dockable window during recording/playback sequence. I am using Sendkeys.Send during playback to simulate key strokes.

             Currently I am facing typical problem during playback with Pause functionality. When I click on pause button sometimes playback sequence does not work properly as some of the simulated keystrokes are not received by active window as focus may be shifted to record palette by user for selecting pause. This happens some times especially if playback speed is high.

               Please note that I am activating the main window before sending keystrokes.

             Can u please advise me on how I can tackle this issue ?

     

    Regards,

    Gokuldas

     

    Wednesday, April 26, 2006 3:13 PM

Answers

  • At an application level,you can handle it by using Application.AddFilterMessage(filter);

    where filter is an object of a class which implements IMessageFilter.

    (filter = new MyFilter());

    private class Myfilter : IMessageFilter{

    public bool PreFilterMessage(ref Message m)

    {

    }

    }

     

    here you can check for the message number for the correspoding windows message and trap it.

    Thursday, April 27, 2006 8:52 AM

All replies

  • At an application level,you can handle it by using Application.AddFilterMessage(filter);

    where filter is an object of a class which implements IMessageFilter.

    (filter = new MyFilter());

    private class Myfilter : IMessageFilter{

    public bool PreFilterMessage(ref Message m)

    {

    }

    }

     

    here you can check for the message number for the correspoding windows message and trap it.

    Thursday, April 27, 2006 8:52 AM
  • Is your problem solved?

    Thank you,
    Bhanu.

    Thursday, May 4, 2006 1:03 AM
  • Can you kindly mark it as an answer?
    Tuesday, May 9, 2006 8:49 AM
  • Hi Kartik,

    Thanks for your reply. I had tried  this earlier. I have used  keypress Up event to trap simulated keystrokes on recordplayback palette and raising repeatcommand event to playback engine as it need to send the command again for appropriate active window. This has stopped mal-functioning of playback commands .

     

    Regards,

    Gokuldas

     

    Wednesday, May 17, 2006 11:52 AM
  • Hi Kartik,

                I am able to get around this problem by implementing IMessageFilter as suggested by you. Because handling it inside RecordPlayback palette form I was able to raise repeat command by trapping  keystrokes  in keyboard even handlers of form but  form was also processing the keystrokes resulting in shifting of focus on  buttons which I wanted to avoid. By handling in PreMessage filter I could  prevent it from reaching recordpalette and raise the repeat command. But this filter was intercepting keystrokes of all other active forms of the application. I made slight changes in the handler to handle only when active window is RecordPlayback Palette. 

    Here is code for TestMessageFilter  class

    using System;

    using System.Windows.Forms;

    namespace SimulateKeyInterceptionDemo {

    // Create message filter

    public class TestMessageFilter : IMessageFilter {

    private TestPanel _owner=null;

    public TestMessageFilter(TestPanel owner) {

    _owner = owner;

    }

    public bool PreFilterMessage(ref Message m) {

    if(!_owner.IsActive) {

    return false;

    }

    if ((m.Msg == KEY_DOWN || m.Msg == KEY_UP || m.Msg == KEY_PRESS)) {

    Console.WriteLine("Trapping Keyboard event and Message is {0}", m.Msg.ToString());

    if (m.Msg == KEY_DOWN) {

    _owner.RaiseRepeateEvent();

    return true;

    }

    return true;

    } else {

    return false;

    }

    }

    public const int KEY_DOWN = 0x0100;

    public const int KEY_UP = 0x0101;

    public const int KEY_PRESS = 0x0102;

    }

    }

     And here is the place I am adding filter

    public TestPanel(Form1 frm) {

    InitializeComponent();

    _frm = frm;

    btnPause.Enabled = true;

    btnStop.Enabled = true;

    btnResume.Enabled = false;

    Application.AddMessageFilter(new TestMessageFilter(this));

    }

    Anyway thanks for your help.

    Now we can close this thread as problem is resolved.

     

    Wednesday, May 17, 2006 3:47 PM