Quantcast
Channel: GlobalMouseKeyHook Discussions Rss Feed
Viewing all 68 articles
Browse latest View live

New Post: Missing interfaces for listeners

$
0
0

If you use interfaces (especially for the listeners) it is much easier for other people to create mocks for unit tests. As a plus the tight coupling to a concrete implementation can be removed (partly).

e.g.:

     /// <summary>
    /// Common interface for handling hooks.
    /// </summary>
    public interface IHookListener
    {
        /// <summary>
        /// Gets or Sets the enabled status of the Hook.
        /// </summary>
        /// <value>
        ///  <c>true</c> if the Hook is presently installed, activated, and will fire events; otherwise, <c>false</c> if the Hook is not part of the hook chain, and will not fire events.
        /// </value>
        bool Enabled { get; set; }
    }

 


    /// <summary>
    /// This is an interface of monitoring keyboard activities.
    /// </summary>
    public interface IKeyboardHookListener : IHookListener
    {
        #region Public Events

        /// <summary>
        /// Occurs when a key is pressed.
        /// </summary>
        event KeyEventHandler KeyDown;

        /// <summary>
        /// Occurs when a key is pressed.
        /// </summary>
        /// <remarks>
        /// Key events occur in the following order:
        /// <list type="number">
        ///   <item>KeyDown</item>
        ///   <item>KeyPress</item>
        ///   <item>KeyUp</item>
        /// </list>
        /// The KeyPress event is not raised by non-character keys; however, the non-character keys do raise the KeyDown and KeyUp events.
        /// Use the KeyChar property to sample keystrokes at run time and to consume or modify a subset of common keystrokes.
        /// To handle keyboard events only in your application and not enable other applications to receive keyboard events,
        /// set the <see cref="KeyPressEventArgs.Handled"/> property in your form's KeyPress event-handling method to <b>true</b>.
        /// </remarks>
        event KeyPressEventHandler KeyPress;

        /// <summary>
        /// Occurs when a key is released.
        /// </summary>
        event KeyEventHandler KeyUp;

        #endregion
    }

 

   /// <summary>
    /// This is an interface of monitoring all mouse activities.
    /// </summary>
    public interface IMouseHookListener : IHookListener
    {
        #region Public Events

        /// <summary>
        /// Occurs when a click was performed by the mouse.
        /// </summary>
        event MouseEventHandler MouseClick;

        /// <summary>
        /// Occurs when a mouse button is double-clicked.
        /// </summary>
        event MouseEventHandler MouseDoubleClick;

        /// <summary>
        /// Occurs when the mouse a mouse button is pressed.
        /// </summary>
        event MouseEventHandler MouseDown;

        /// <summary>
        /// Occurs when the mouse pointer is moved.
        /// </summary>
        event MouseEventHandler MouseMove;

        /// <summary>
        /// Occurs when a mouse button is released.
        /// </summary>
        event MouseEventHandler MouseUp;

        /// <summary>
        /// Occurs when the mouse wheel moves.
        /// </summary>
        event MouseEventHandler MouseWheel;

        #endregion
    }

 


New Post: x86 and x64 => AnyCPU

$
0
0

A solution to remove the #ifdef for the FieldOffset property might be to split up the AppMouseStruct class into two separate structs (one for x86 and one for x64) because they are only used by one Method and get converted into a MouseStruct.

1. Split up the struct AppMouseStruct in x86 and x64

    [StructLayout(LayoutKind.Explicit)]
    internal struct AppMouseStructForX86
    {

        /// <summary>
        /// Specifies a Point structure that contains the X- and Y-coordinates of the cursor, in screen coordinates.
        /// </summary>
        [FieldOffset(0x00)]
        public Point Point;

        /// <summary>
        /// Specifies information associated with the message.
        /// </summary>
        /// <remarks>
        /// The possible values are:
        /// <list type="bullet">
        /// <item>
        /// <description>0 - No Information</description>
        /// </item>
        /// <item>
        /// <description>1 - X-Button1 Click</description>
        /// </item>
        /// <item>
        /// <description>2 - X-Button2 Click</description>
        /// </item>
        /// <item>
        /// <description>120 - Mouse Scroll Away from User</description>
        /// </item>
        /// <item>
        /// <description>-120 - Mouse Scroll Toward User</description>
        /// </item>
        /// </list>
        /// </remarks>
        [FieldOffset(0x16)]
        public Int16 MouseData;

        /// <summary>
        /// Converts the current <see cref="AppMouseStructForX86"/> into a <see cref="MouseStruct"/>.
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// The AppMouseStruct does not have a timestamp, thus one is generated at the time of this call.
        /// </remarks>
        public MouseStruct ToMouseStruct()
        {
            var tmp = new MouseStruct();
            tmp.Point = this.Point;
            tmp.MouseData = this.MouseData;
            tmp.Timestamp = Environment.TickCount;
            return tmp;
        }
    }

 

    /// <summary>
    /// The AppMouseStructForX64 structure contains information about a application-level mouse input event for x64.
    /// </summary>
    /// <remarks>
    /// See full documentation at http://globalmousekeyhook.codeplex.com/wikipage?title=MouseStruct
    /// </remarks>
    [StructLayout(LayoutKind.Explicit)]
    internal struct AppMouseStructForX64
    {

        /// <summary>
        /// Specifies a Point structure that contains the X- and Y-coordinates of the cursor, in screen coordinates.
        /// </summary>
        [FieldOffset(0x00)]
        public Point Point;

        /// <summary>
        /// Specifies information associated with the message.
        /// </summary>
        /// <remarks>
        /// The possible values are:
        /// <list type="bullet">
        /// <item>
        /// <description>0 - No Information</description>
        /// </item>
        /// <item>
        /// <description>1 - X-Button1 Click</description>
        /// </item>
        /// <item>
        /// <description>2 - X-Button2 Click</description>
        /// </item>
        /// <item>
        /// <description>120 - Mouse Scroll Away from User</description>
        /// </item>
        /// <item>
        /// <description>-120 - Mouse Scroll Toward User</description>
        /// </item>
        /// </list>
        /// </remarks>
        [FieldOffset(0x22)]
        public Int16 MouseData;

        /// <summary>
        /// Converts the current <see cref="MouseStruct"/> into a <see cref="AppMouseStructForX64"/>.
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// The AppMouseStruct does not have a timestamp, thus one is generated at the time of this call.
        /// </remarks>
        public MouseStruct ToMouseStruct()
        {
            MouseStruct tmp = new MouseStruct();
            tmp.Point = this.Point;
            tmp.MouseData = this.MouseData;
            tmp.Timestamp = Environment.TickCount;
            return tmp;
        }
    }

2. .... and for the MouseEventExtArgs class modify the FramRawDataApp method as follows:

        /// <summary>
        /// Creates <see cref="MouseEventExtArgs"/> from Windows Message parameters,
        /// based upon a local application hook.
        /// </summary>
        /// <param name="wParam">The first Windows Message parameter.</param>
        /// <param name="lParam">The second Windows Message parameter.</param>
        /// <returns>A new MouseEventExtArgs object.</returns>
        private static MouseEventExtArgs FromRawDataApp(int wParam, IntPtr lParam)
        {
            if (IntPtr.Size != 4)
            {
                var marshalledMouseStruct = (AppMouseStructForX64)Marshal.PtrToStructure(lParam, typeof(AppMouseStructForX64));
                return FromRawDataUniversal(wParam, marshalledMouseStruct.ToMouseStruct());
            }
            else
            {
                var marshalledMouseStruct = (AppMouseStructForX86)Marshal.PtrToStructure(lParam, typeof(AppMouseStructForX86));
                return FromRawDataUniversal(wParam, marshalledMouseStruct.ToMouseStruct());
            }
        }

... this way together with the (IntPtr.Size == 4) ? "x86":"x64") hint from the post above there is no need for separate builds for x86 and x64... so you're lib users don't get a "BadImageException" because they're loading the wrong assembly. ;-)

 

New Post: Mouseclick does not suppress in dirextx9 game

$
0
0

Hi guys.

For testing only, i've made a small application that suppress left click once the event is triggered.

Ive tested with both "MouseClick" and "MouseDown" though in a DirectX9 game (currently windowed) the suppression does not work.

Do anyone have any suggestion in order to make the suppression work also when the game is active?

New Post: StyleCop guidelines

$
0
0

Adding StyleCop guidelines would improve the code (reading) quality. I suggest to add such a rule file and use StyleCop/Resharper to polish the code.

New Post: Mouseclick does not suppress in dirextx9 game

$
0
0

hi again,

i've searched the entire web without luck.
Im able to suppress keyboard without any problem, mouse movement (by skipping the NextCall... return) though mouseclicks are still working inside the directx game.

I would really appreciate some suggestions to what I could try :)

 

New Post: Application and Global Mouse and Keyboard Hooks .Net Libary in C#

$
0
0

Hi ,

   Is it possible to get the code for "KeyEventExtArgs" where I will be able to get the timestamp of the key pressed and released ?

Is it possible to include different applications name (used for typing activity) during the hook time ?

 

 

Thanks

Rajat

New Post: x86 and x64 => AnyCPU

$
0
0

Thanks for posting this!  Worked perfectly on a app I am building with VS2012.

New Post: 2 Questions (usage, and license)

$
0
0

Hi - this is a great project, thank you for putting all this work into it.  I have two questions.

1) Licensing.  I am thinking about reusing the binary in a commercial project - is this permitted?  I do not plan to modify the code.  The binaries will be bundled with and accessed from another application that I am writing.  According to the license page, it seems as though putting the license into the documentation or ReadMe is sufficient to meet the terms of use, but I want to make sure.

2) My application is configured to compile for 'any cpu' because it needs to run on both x86 and x64  versions of Win7+.  I noticed that the binary download includes both x86 and x64 dlls.  What is the recommended approach for handling this?  I would like for my app to run on both x86 and x64 but worst case it should run on x64. 

Any help appreciated.

Miguel


New Post: 2 Questions (usage, and license)

$
0
0

Hi Miguel,

1) As you noticed correctly distributing as a binary along with a commercial product is allowed under conditions mentioned in LICENSE section. It basically means including name of the project, its URL and license text in readme.

2) Currently ANY CPU is not supported. There where some solution proposals see: [discussion:40238]. Unfortunatelly no one has implemented and tested it inside the product, so current release do not support ANY CPU and you need to use x86 or x64 dll conditionally. ANY CPU feature is one of the most requeseted and will be probably included in next releas. Unfortunatelly I can not say anything about the timeframe.

Regards
George 

 

New Post: Keyhook while pc is locked

$
0
0

Hello,

I have got a short question:

Is it possible to get keyhooks while pc is locked.

In my experience it is not possible ?

 

Regards,

Matthias

New Post: Application and Global Mouse and Keyboard Hooks .Net Libary in C#

$
0
0

This is a Great Job!!!

I have implemented the dll into a visual 2010 vb.net project, and it works well.

I am trying to monitor the keystrokes so that I can suppress certain keystrokes from another running application.  Unfortunately from all the information I have found, none provides any insight for suppress specific keystrokes.

Is this available in the library?  If so, please provide some insight, a simple example would be very welcome.

Thanks

Chuck 

New Post: Is is possible to recognise which keyboard was used?

$
0
0

I'm trying to make an application which can capture the keys of two keypads I have attached to my pc. I'd like to be able to recognize which keypad caused the event.

I'd like to know if this is somehow possible with this library, for instance by capturing the HID device ID.

New Post: Capture ONLY ENTER key

$
0
0

Hi,

I only want to capture the ENTER key, is it possible?

Say for instance, if user press "123" followed by the ENTER, the count would trigger 1, not 4.

And the next instance if user press 456, followed by the ENTER, the count would trigger 2.

Is it possible?

I try to put under one of those Key event, like HookManager_KeyUp, it counts also non-ENTER keys, which I do not want. So where should I put my code under?

  public partial class TestFormHookListeners : Form
    {
        private readonly KeyboardHookListener m_KeyboardHookManager;
        private readonly MouseHookListener m_MouseHookManager;
        int count=1;

  private void HookManager_KeyUp(object sender, KeyEventArgs e)
        {
            Log(string.Format("KeyUp  \t\t {0}\n", e.KeyCode));
            if (e.KeyCode == Keys.Enter)
            {
                Count_textBox.Text = count.ToString();
            }
            count = count + 1;
        }

}

 

New Post: When does Win7 remove a hook from the chain?

$
0
0

Interesting.

I might be experiencing a similar problem. Did you find out anything more about this?

New Post: When does Win7 remove a hook from the chain?

$
0
0

Related: http://stackoverflow.com/questions/2655278/what-can-cause-windows-to-unhook-a-low-level-global-keyboard-hook

Under Remarks on msdn: http://msdn.microsoft.com/en-us/library/ms644985.aspx


New Post: When does Win7 remove a hook from the chain?

$
0
0

The accepted answer on msdn might be the cleanest approach?

http://social.msdn.microsoft.com/Forums/en-US/windowscompatibility/thread/f6032ca1-31b8-4ad5-be39-f78dd29952da

New Post: Mouseclick does not suppress in dirextx9 game

$
0
0
Hi, MRmP,
ive gotten a problem similiar with yours and do you find any solution recently?
it would be nice if you got any progress:)

Thx anyway!

New Post: Extending this to touch gestures of touchpad in laptop

$
0
0
Hi Aditya,

i am finding same thing. if you got any answer than please help me. I want to capture touch events in surface machine.
    private void button1_Click(object sender, EventArgs e)//Start hook
    {
        InitializeSurfaceInput();
    }
     private void InitializeSurfaceInput() 
    {
            // Create a target for surface input.
        touchTarget = new TouchTarget(IntPtr.Zero, EventThreadChoice.OnBackgroundThread);
        touchTarget.EnableInput();

        touchTarget.TouchDown += new EventHandler<TouchEventArgs>(touchTarget_TouchDown);
        touchTarget.TouchMove += new EventHandler<TouchEventArgs>(touchTarget_TouchMove);
    }

    void touchTarget_TouchMove(object sender, TouchEventArgs e)
    {
        Console.WriteLine(e.ToString());
    }

    void touchTarget_TouchDown(object sender, TouchEventArgs e)
    {

        Console.WriteLine(e.ToString());

    }

    public TouchTarget touchTarget { get; set; }

    private void button2_Click(object sender, EventArgs e) //Stop Hook
    {
        touchTarget.TouchDown -= new EventHandler<TouchEventArgs>(touchTarget_TouchDown);
    }

New Post: SendKeys.SendWait("^c") not working correctly in private void HookManager_MouseDoubleClick(object sender, MouseEventArgs e)

$
0
0
I want to get selected text when I double click text in other application. So, I customize the HookManager_MouseDoubleClick method. But the SendKeys.SendWait("^c") didn't work when putting it in HookManager_MouseDoubleClick method , I didn't get the selected text. With the same way, I customize the http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C, It works correctly. I don't know the reason why SendKeys.SendWait("^c") doesn't work in this project.Please help me.Thank in advance!

//my code
private void HookManager_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        Clipboard.Clear();
       SendKeys.SendWait("^c");
        Log(string.Format("MouseDoubleClick \t\t {0}\n", Clipboard.GetText()));
    }

New Post: Keyhook while pc is locked

Viewing all 68 articles
Browse latest View live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>