Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Mouse Button > keystroke

Mouse Button > keystroke

Scheduled Pinned Locked Moved C#
tutorial
17 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Anonymous

    as.thakor@ntlworld.com could i speak to you by email direct ? it would be greatly appreciated Thanks Atul

    K Offline
    K Offline
    Kentamanos
    wrote on last edited by
    #7

    Here's a chunk of code I wrote. This class was basically being hosted by remoting (thus the MarshalByRefObject), but it moves the mouse where you want it. It shows how to interop with SendInput (importing the definition and defining a few structures you'll need).

    using System;
    using System.Runtime.InteropServices;

    [StructLayout(LayoutKind.Explicit, Size=28)]
    public struct Input
    {
      [FieldOffset (0)]
      public uint  type;
      [FieldOffset (4)]
      public MouseInput mi;
      [FieldOffset (4)]
      public KeyboardInput ki;
      [FieldOffset (4)]
      public HardwareInput hi;

    public const uint INPUT_MOUSE    = 0;
      public const uint INPUT_KEYBOARD  = 1;
      public const uint INPUT_HARDWARE  = 2;
     }

    [StructLayout(LayoutKind.Sequential, Size=24)]
    public struct MouseInput
    {
      public int  dx;
      public int  dy;
      public uint  mouseData;
      public uint

    A S 2 Replies Last reply
    0
    • K Kentamanos

      Here's a chunk of code I wrote. This class was basically being hosted by remoting (thus the MarshalByRefObject), but it moves the mouse where you want it. It shows how to interop with SendInput (importing the definition and defining a few structures you'll need).

      using System;
      using System.Runtime.InteropServices;

      [StructLayout(LayoutKind.Explicit, Size=28)]
      public struct Input
      {
        [FieldOffset (0)]
        public uint  type;
        [FieldOffset (4)]
        public MouseInput mi;
        [FieldOffset (4)]
        public KeyboardInput ki;
        [FieldOffset (4)]
        public HardwareInput hi;

      public const uint INPUT_MOUSE    = 0;
        public const uint INPUT_KEYBOARD  = 1;
        public const uint INPUT_HARDWARE  = 2;
       }

      [StructLayout(LayoutKind.Sequential, Size=24)]
      public struct MouseInput
      {
        public int  dx;
        public int  dy;
        public uint  mouseData;
        public uint

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #8

      Your a star!

      1 Reply Last reply
      0
      • K Kentamanos

        Here's a chunk of code I wrote. This class was basically being hosted by remoting (thus the MarshalByRefObject), but it moves the mouse where you want it. It shows how to interop with SendInput (importing the definition and defining a few structures you'll need).

        using System;
        using System.Runtime.InteropServices;

        [StructLayout(LayoutKind.Explicit, Size=28)]
        public struct Input
        {
          [FieldOffset (0)]
          public uint  type;
          [FieldOffset (4)]
          public MouseInput mi;
          [FieldOffset (4)]
          public KeyboardInput ki;
          [FieldOffset (4)]
          public HardwareInput hi;

        public const uint INPUT_MOUSE    = 0;
          public const uint INPUT_KEYBOARD  = 1;
          public const uint INPUT_HARDWARE  = 2;
         }

        [StructLayout(LayoutKind.Sequential, Size=24)]
        public struct MouseInput
        {
          public int  dx;
          public int  dy;
          public uint  mouseData;
          public uint

        S Offline
        S Offline
        Spanky3
        wrote on last edited by
        #9

        I have just started playing with the code and read that its possible to make a global hook :( http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q319524 :(

        K 1 Reply Last reply
        0
        • S Spanky3

          I have just started playing with the code and read that its possible to make a global hook :( http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q319524 :(

          K Offline
          K Offline
          Kentamanos
          wrote on last edited by
          #10

          I was wondering how that was going to work (I'm actually not sure the exact purpose of installing a hook that's not global to be honest...I need to read more about that). I knew you had to write a DLL with a specific signature that gets loaded by every application (I wrote one once for a kiosk that disabled and enabled things like CTRL-ALT-DELETE once they typed in a password). I looks like almost the entire project would have to be a straight Win32 DLL in C/C++ (since the keyboard hook would be calling SendInput). You'd most likely write an application that installs and uninstalls the hook as well (which wouldn't necessarily have to be written in C/C++). Have you ever done any C++? This DLL wouldn't actually be that big...you just have to get it perfect since everything is calling it ;). If I get a chance tonight, I'll play around with writing a hook (busy at work at the moment).


          I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
          -David St. Hubbins

          S 3 Replies Last reply
          0
          • K Kentamanos

            I was wondering how that was going to work (I'm actually not sure the exact purpose of installing a hook that's not global to be honest...I need to read more about that). I knew you had to write a DLL with a specific signature that gets loaded by every application (I wrote one once for a kiosk that disabled and enabled things like CTRL-ALT-DELETE once they typed in a password). I looks like almost the entire project would have to be a straight Win32 DLL in C/C++ (since the keyboard hook would be calling SendInput). You'd most likely write an application that installs and uninstalls the hook as well (which wouldn't necessarily have to be written in C/C++). Have you ever done any C++? This DLL wouldn't actually be that big...you just have to get it perfect since everything is calling it ;). If I get a chance tonight, I'll play around with writing a hook (busy at work at the moment).


            I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
            -David St. Hubbins

            S Offline
            S Offline
            Spanky3
            wrote on last edited by
            #11

            i got hold of a dll hook someone used in VB maybe its convertable to be used in .net ? http://www.allapi.net/php/redirect/redirect.php?action=download&id=372 thats the link to the sample prog and .dll nope i havent much experience in c++ only the very basics of c but i am willing to learn :)

            K 1 Reply Last reply
            0
            • K Kentamanos

              I was wondering how that was going to work (I'm actually not sure the exact purpose of installing a hook that's not global to be honest...I need to read more about that). I knew you had to write a DLL with a specific signature that gets loaded by every application (I wrote one once for a kiosk that disabled and enabled things like CTRL-ALT-DELETE once they typed in a password). I looks like almost the entire project would have to be a straight Win32 DLL in C/C++ (since the keyboard hook would be calling SendInput). You'd most likely write an application that installs and uninstalls the hook as well (which wouldn't necessarily have to be written in C/C++). Have you ever done any C++? This DLL wouldn't actually be that big...you just have to get it perfect since everything is calling it ;). If I get a chance tonight, I'll play around with writing a hook (busy at work at the moment).


              I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
              -David St. Hubbins

              S Offline
              S Offline
              Spanky3
              wrote on last edited by
              #12

              http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/default.aspx Apparently a Global hook....... doesnt seem to do much though :-O:-O:-O

              1 Reply Last reply
              0
              • S Spanky3

                i got hold of a dll hook someone used in VB maybe its convertable to be used in .net ? http://www.allapi.net/php/redirect/redirect.php?action=download&id=372 thats the link to the sample prog and .dll nope i havent much experience in c++ only the very basics of c but i am willing to learn :)

                K Offline
                K Offline
                Kentamanos
                wrote on last edited by
                #13

                That app uses Visual Basic (not VB.NET) to create a DLL. His DLL might have even been written in C/C++, but in any event you can specify exactly how the DLL should look in any of those languages (some more easier than others) I could be wrong (wouldn't be the first time), but I don't think you can create a .NET "Win32 Dll" (there might be some third party stuff that lets you do this, but I don't know). That is to say, I don't think you can create something in .NET that's just a DLL (and not a managed assembly). You can create a DLL that exposes COM functionality, but I don't think you can get as specific as you need for this DLL in .NET (either VB.NET or C#). I think this job would actually require something other than .NET.


                I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
                -David St. Hubbins

                1 Reply Last reply
                0
                • K Kentamanos

                  I was wondering how that was going to work (I'm actually not sure the exact purpose of installing a hook that's not global to be honest...I need to read more about that). I knew you had to write a DLL with a specific signature that gets loaded by every application (I wrote one once for a kiosk that disabled and enabled things like CTRL-ALT-DELETE once they typed in a password). I looks like almost the entire project would have to be a straight Win32 DLL in C/C++ (since the keyboard hook would be calling SendInput). You'd most likely write an application that installs and uninstalls the hook as well (which wouldn't necessarily have to be written in C/C++). Have you ever done any C++? This DLL wouldn't actually be that big...you just have to get it perfect since everything is calling it ;). If I get a chance tonight, I'll play around with writing a hook (busy at work at the moment).


                  I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
                  -David St. Hubbins

                  S Offline
                  S Offline
                  Spanky3
                  wrote on last edited by
                  #14

                  Google 2 Win it seems http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=872&lngWId=10 has the source code for someone who has created something pretty much what i want in vb.net just gotta translate ye old code to c# now or just use the vb.net stuff if your still intrested in having a mess about with this project your very welcome :) as your input has been extremely value!

                  S K 2 Replies Last reply
                  0
                  • S Spanky3

                    Google 2 Win it seems http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=872&lngWId=10 has the source code for someone who has created something pretty much what i want in vb.net just gotta translate ye old code to c# now or just use the vb.net stuff if your still intrested in having a mess about with this project your very welcome :) as your input has been extremely value!

                    S Offline
                    S Offline
                    Spanky3
                    wrote on last edited by
                    #15

                    k had a look at the source........ Private Declare Function GetCursor Lib "user32" () As Int32 Private Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (ByRef lpPoint As POINTAPI) As Int32 Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Int32, ByVal yPoint As Int32) As Int32 Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Int32, ByRef lpdwProcessId As Int32) As Int32 Private Declare Function AttachThreadInput Lib "user32" Alias "AttachThreadInput" (ByVal idAttach As Int32, ByVal idAttachTo As Int32, ByVal fAttach As Int32) As Int32 Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Int32) As Int16 k i understand GetCursor is the function name and it returns in Integer ....... but "user32" refers to a library and i have no idea how to port that part of the code over to c# :-O just out of interest is this...... code actually intercepting the events or simply reading them as they fly by as i need to read process and act

                    1 Reply Last reply
                    0
                    • S Spanky3

                      Google 2 Win it seems http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=872&lngWId=10 has the source code for someone who has created something pretty much what i want in vb.net just gotta translate ye old code to c# now or just use the vb.net stuff if your still intrested in having a mess about with this project your very welcome :) as your input has been extremely value!

                      K Offline
                      K Offline
                      Kentamanos
                      wrote on last edited by
                      #16

                      Not sure if that technique would work for DirectX games etc. It might not be low level enough. I think hooks might be the only way to do it, and doing global hooks seems truly impossible to do entirely in .NET. It's not too hard to get unmanaged code (C/C++/VB) to talk to managed code via a COM. That might be the easiest way: create a C/C++ hook, create a .NET class that gets wrapped as a COM component, let the hook call the .NET class. This would allow you to do most of your development in C#, but I think some C/C++ is going to be unavoidable.


                      I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
                      -David St. Hubbins

                      S 1 Reply Last reply
                      0
                      • K Kentamanos

                        Not sure if that technique would work for DirectX games etc. It might not be low level enough. I think hooks might be the only way to do it, and doing global hooks seems truly impossible to do entirely in .NET. It's not too hard to get unmanaged code (C/C++/VB) to talk to managed code via a COM. That might be the easiest way: create a C/C++ hook, create a .NET class that gets wrapped as a COM component, let the hook call the .NET class. This would allow you to do most of your development in C#, but I think some C/C++ is going to be unavoidable.


                        I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
                        -David St. Hubbins

                        S Offline
                        S Offline
                        Spanky3
                        wrote on last edited by
                        #17

                        http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1052&lngWId=10 This one is definately low level enough :D seems it anyway Public Class frmMain Inherits System.Windows.Forms.Form Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, ByVal hMod As Integer, ByVal dwThreadId As Integer) As Integer Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal hHook As Integer) As Integer Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer Const WH_KEYBOARD_LL = 13 Structure KBDLLHOOKSTRUCT Dim vkCode As Integer Dim scanCode As Integer Dim flags As Integer Dim time As Integer Dim dwExtraInfo As Integer End Structure Dim intLLKey As Integer #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Private Sub InitializeComponent() Me.lblInfo = New System.Windows.Forms.Label() Me.SuspendLayout() ' 'lblInfo ' Me.lblInfo.Location = New System.Drawing.Point(16, 16) Me.lblInfo.Name = "lblInfo" Me.lblInfo.Size = New System.Drawing.Size(216, 88) Me.lblInfo.TabIndex = 0 Me.lblInfo.Text = "Alt-Tab, Alt-Esc, Ctrl-Esc, and the Windows key are disabled. C

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups