Mouse Hook in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Logan Mckinley

    Mouse Hook in C#

    I need to know where the cursor is (x,y) and when it moves even when it is
    not over my form. I know i can get the current location with:
    System.Windows. Forms.Cursor.Po sition.X
    System.Windows. Forms.Cursor.Po sition.X
    But that does not raise an event and using a timer would be to processor
    intensive. In MFC you would write a mousehook to solve this problem but when
    i run the C# MouseHook code for from MSDN
    (http://support.microsoft.com/?kbid=318804) it only seems to return anything
    when the mouse is over the form. I am guessing i am doing something wrong
    with the MouseHook.
    Thanks in advance,
    ~Logan


  • Chris Taylor

    #2
    Re: Mouse Hook in C#

    Hi,

    To achieve this you will have to install a system wide hook. As per the MSDN
    documentation this is not possible using a managed assembly. You will have
    to do this using C/C++ and then one option might be to expose an interface
    that marshals the information back to the managed application.

    Hope this helps

    Chris Taylor

    "Logan Mckinley" <logan@globalwe b.net> wrote in message
    news:eCN5K%23rl DHA.2456@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > I need to know where the cursor is (x,y) and when it moves even when it is
    > not over my form. I know i can get the current location with:
    > System.Windows. Forms.Cursor.Po sition.X
    > System.Windows. Forms.Cursor.Po sition.X
    > But that does not raise an event and using a timer would be to processor
    > intensive. In MFC you would write a mousehook to solve this problem but[/color]
    when[color=blue]
    > i run the C# MouseHook code for from MSDN
    > (http://support.microsoft.com/?kbid=318804) it only seems to return[/color]
    anything[color=blue]
    > when the mouse is over the form. I am guessing i am doing something wrong
    > with the MouseHook.
    > Thanks in advance,
    > ~Logan
    >
    >[/color]


    Comment

    • anonymouse

      #3
      Re: Mouse Hook in C#

      I presume they plan on filling these huge gaps in the libraries?


      "Chris Taylor" <chris_taylor_z a@hotmail.com> wrote in message
      news:#P3FYNvlDH A.2528@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Hi,
      >
      > To achieve this you will have to install a system wide hook. As per the[/color]
      MSDN[color=blue]
      > documentation this is not possible using a managed assembly. You will[/color]
      have[color=blue]
      > to do this using C/C++ and then one option might be to expose an interface
      > that marshals the information back to the managed application.
      >
      > Hope this helps
      >
      > Chris Taylor
      >
      > "Logan Mckinley" <logan@globalwe b.net> wrote in message
      > news:eCN5K%23rl DHA.2456@TK2MSF TNGP09.phx.gbl. ..[color=green]
      > > I need to know where the cursor is (x,y) and when it moves even when it[/color][/color]
      is[color=blue][color=green]
      > > not over my form. I know i can get the current location with:
      > > System.Windows. Forms.Cursor.Po sition.X
      > > System.Windows. Forms.Cursor.Po sition.X
      > > But that does not raise an event and using a timer would be to processor
      > > intensive. In MFC you would write a mousehook to solve this problem but[/color]
      > when[color=green]
      > > i run the C# MouseHook code for from MSDN
      > > (http://support.microsoft.com/?kbid=318804) it only seems to return[/color]
      > anything[color=green]
      > > when the mouse is over the form. I am guessing i am doing something[/color][/color]
      wrong[color=blue][color=green]
      > > with the MouseHook.
      > > Thanks in advance,
      > > ~Logan
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Chris Taylor

        #4
        Re: Mouse Hook in C#

        Not being a MS Insider I can only guess, but with time I am sure more and
        more of the missing functiontionali ty will be addressed. However I think
        that .NET is targeting application development and for a long time to come
        we will be using C/C++ to write the more system level type code. While .NET
        will provide a more secure environment for application level tasks.

        Those are just my thoughts

        Chris Taylor
        "anonymouse " <anonymouse@dis cussions.micros oft.com> wrote in message
        news:O0gK0SvlDH A.2676@TK2MSFTN GP11.phx.gbl...[color=blue]
        > I presume they plan on filling these huge gaps in the libraries?
        >
        >
        > "Chris Taylor" <chris_taylor_z a@hotmail.com> wrote in message
        > news:#P3FYNvlDH A.2528@TK2MSFTN GP12.phx.gbl...[color=green]
        > > Hi,
        > >
        > > To achieve this you will have to install a system wide hook. As per the[/color]
        > MSDN[color=green]
        > > documentation this is not possible using a managed assembly. You will[/color]
        > have[color=green]
        > > to do this using C/C++ and then one option might be to expose an[/color][/color]
        interface[color=blue][color=green]
        > > that marshals the information back to the managed application.
        > >
        > > Hope this helps
        > >
        > > Chris Taylor
        > >
        > > "Logan Mckinley" <logan@globalwe b.net> wrote in message
        > > news:eCN5K%23rl DHA.2456@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
        > > > I need to know where the cursor is (x,y) and when it moves even when[/color][/color][/color]
        it[color=blue]
        > is[color=green][color=darkred]
        > > > not over my form. I know i can get the current location with:
        > > > System.Windows. Forms.Cursor.Po sition.X
        > > > System.Windows. Forms.Cursor.Po sition.X
        > > > But that does not raise an event and using a timer would be to[/color][/color][/color]
        processor[color=blue][color=green][color=darkred]
        > > > intensive. In MFC you would write a mousehook to solve this problem[/color][/color][/color]
        but[color=blue][color=green]
        > > when[color=darkred]
        > > > i run the C# MouseHook code for from MSDN
        > > > (http://support.microsoft.com/?kbid=318804) it only seems to return[/color]
        > > anything[color=darkred]
        > > > when the mouse is over the form. I am guessing i am doing something[/color][/color]
        > wrong[color=green][color=darkred]
        > > > with the MouseHook.
        > > > Thanks in advance,
        > > > ~Logan
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Logan Mckinley

          #5
          Re: Mouse Hook in C#

          So does the C# MouseHook per the MSDN article i posted catch mouse movements
          across all of the elements of your form?
          Thanks,
          ~Logan


          "Chris Taylor" <chris_taylor_z a@hotmail.com> wrote in message
          news:%23P3FYNvl DHA.2528@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > Hi,
          >
          > To achieve this you will have to install a system wide hook. As per the[/color]
          MSDN[color=blue]
          > documentation this is not possible using a managed assembly. You will[/color]
          have[color=blue]
          > to do this using C/C++ and then one option might be to expose an interface
          > that marshals the information back to the managed application.
          >
          > Hope this helps
          >
          > Chris Taylor
          >
          > "Logan Mckinley" <logan@globalwe b.net> wrote in message
          > news:eCN5K%23rl DHA.2456@TK2MSF TNGP09.phx.gbl. ..[color=green]
          > > I need to know where the cursor is (x,y) and when it moves even when it[/color][/color]
          is[color=blue][color=green]
          > > not over my form. I know i can get the current location with:
          > > System.Windows. Forms.Cursor.Po sition.X
          > > System.Windows. Forms.Cursor.Po sition.X
          > > But that does not raise an event and using a timer would be to processor
          > > intensive. In MFC you would write a mousehook to solve this problem but[/color]
          > when[color=green]
          > > i run the C# MouseHook code for from MSDN
          > > (http://support.microsoft.com/?kbid=318804) it only seems to return[/color]
          > anything[color=green]
          > > when the mouse is over the form. I am guessing i am doing something[/color][/color]
          wrong[color=blue][color=green]
          > > with the MouseHook.
          > > Thanks in advance,
          > > ~Logan
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Chris Taylor

            #6
            Re: Mouse Hook in C#

            Hi,

            Yes, just to be certain I took an existing form with a multiline edit
            control, three buttons a menu and a single line edit control plug the code
            into the form and no matter where I moved with in the confines of the form
            the hook function was called.

            Chris Taylor

            "Logan Mckinley" <logan@globalwe b.net> wrote in message
            news:eUHAkawlDH A.2272@tk2msftn gp13.phx.gbl...[color=blue]
            > So does the C# MouseHook per the MSDN article i posted catch mouse[/color]
            movements[color=blue]
            > across all of the elements of your form?
            > Thanks,
            > ~Logan
            >
            >
            > "Chris Taylor" <chris_taylor_z a@hotmail.com> wrote in message
            > news:%23P3FYNvl DHA.2528@TK2MSF TNGP12.phx.gbl. ..[color=green]
            > > Hi,
            > >
            > > To achieve this you will have to install a system wide hook. As per the[/color]
            > MSDN[color=green]
            > > documentation this is not possible using a managed assembly. You will[/color]
            > have[color=green]
            > > to do this using C/C++ and then one option might be to expose an[/color][/color]
            interface[color=blue][color=green]
            > > that marshals the information back to the managed application.
            > >
            > > Hope this helps
            > >
            > > Chris Taylor
            > >
            > > "Logan Mckinley" <logan@globalwe b.net> wrote in message
            > > news:eCN5K%23rl DHA.2456@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
            > > > I need to know where the cursor is (x,y) and when it moves even when[/color][/color][/color]
            it[color=blue]
            > is[color=green][color=darkred]
            > > > not over my form. I know i can get the current location with:
            > > > System.Windows. Forms.Cursor.Po sition.X
            > > > System.Windows. Forms.Cursor.Po sition.X
            > > > But that does not raise an event and using a timer would be to[/color][/color][/color]
            processor[color=blue][color=green][color=darkred]
            > > > intensive. In MFC you would write a mousehook to solve this problem[/color][/color][/color]
            but[color=blue][color=green]
            > > when[color=darkred]
            > > > i run the C# MouseHook code for from MSDN
            > > > (http://support.microsoft.com/?kbid=318804) it only seems to return[/color]
            > > anything[color=darkred]
            > > > when the mouse is over the form. I am guessing i am doing something[/color][/color]
            > wrong[color=green][color=darkred]
            > > > with the MouseHook.
            > > > Thanks in advance,
            > > > ~Logan
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...