Skip to content

Commit 19a2549

Browse files
authored
Merge pull request #132 from AliRezaBeigy/master
fix: mouse movement issue -> Thanks a lot to @AliRezaBeigy
2 parents c6bcae4 + 7853a72 commit 19a2549

File tree

3 files changed

+138
-33
lines changed

3 files changed

+138
-33
lines changed

src/GregsStack.InputSimulatorStandard/InputBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ public InputBuilder AddAbsoluteMouseMovement(int absoluteX, int absoluteY)
274274
{
275275
var movement = new Input { Type = (uint)InputType.Mouse };
276276
movement.Data.Mouse.Flags = (uint)(MouseFlag.Move | MouseFlag.Absolute);
277-
movement.Data.Mouse.X = absoluteX;
278-
movement.Data.Mouse.Y = absoluteY;
277+
movement.Data.Mouse.X = (int)((absoluteX * 65536) / NativeMethods.GetSystemMetrics((uint)SystemMetric.SM_CXSCREEN));
278+
movement.Data.Mouse.Y = (int)((absoluteY * 65536) / NativeMethods.GetSystemMetrics((uint)SystemMetric.SM_CYSCREEN));
279279

280280
this.inputList.Add(movement);
281281

src/GregsStack.InputSimulatorStandard/Native/NativeMethods.cs

+34-31
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,35 @@ internal static class NativeMethods
1313
/// </summary>
1414
/// <param name="virtualKeyCode">Specifies one of 256 possible virtual-key codes. For more information, see Virtual Key Codes. Windows NT/2000/XP: You can use left- and right-distinguishing constants to specify certain keys. See the Remarks section for further information.</param>
1515
/// <returns>
16-
/// If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.
17-
///
18-
/// Windows NT/2000/XP: The return value is zero for the following cases:
16+
/// If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.
17+
///
18+
/// Windows NT/2000/XP: The return value is zero for the following cases:
1919
/// - The current desktop is not the active desktop
2020
/// - The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.
21-
///
21+
///
2222
/// Windows 95/98/Me: The return value is the global asynchronous key state for each virtual key. The system does not check which thread has the keyboard focus.
23-
///
24-
/// Windows 95/98/Me: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.
23+
///
24+
/// Windows 95/98/Me: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.
2525
/// </returns>
2626
/// <remarks>
27-
/// The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling
27+
/// The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling
2828
/// Copy CodeGetSystemMetrics(SM_SWAPBUTTON) which returns TRUE if the mouse buttons have been swapped.
29-
///
29+
///
3030
/// Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call <see cref="GetAsyncKeyState"/> and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.
31-
///
32-
/// You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the SHIFT, CTRL, or ALT keys without distinguishing between left and right.
33-
///
34-
/// Windows NT/2000/XP: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys.
35-
///
36-
/// Code Meaning
37-
/// VK_LSHIFT Left-shift key.
38-
/// VK_RSHIFT Right-shift key.
39-
/// VK_LCONTROL Left-control key.
40-
/// VK_RCONTROL Right-control key.
41-
/// VK_LMENU Left-menu key.
42-
/// VK_RMENU Right-menu key.
43-
///
44-
/// These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.
31+
///
32+
/// You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the SHIFT, CTRL, or ALT keys without distinguishing between left and right.
33+
///
34+
/// Windows NT/2000/XP: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys.
35+
///
36+
/// Code Meaning
37+
/// VK_LSHIFT Left-shift key.
38+
/// VK_RSHIFT Right-shift key.
39+
/// VK_LCONTROL Left-control key.
40+
/// VK_RCONTROL Right-control key.
41+
/// VK_LMENU Left-menu key.
42+
/// VK_RMENU Right-menu key.
43+
///
44+
/// These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.
4545
/// </remarks>
4646
[DllImport("user32.dll", SetLastError = true)]
4747
public static extern short GetAsyncKeyState(ushort virtualKeyCode);
@@ -50,27 +50,27 @@ internal static class NativeMethods
5050
/// The <see cref="GetKeyState"/> function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off alternating each time the key is pressed). (See: http://msdn.microsoft.com/en-us/library/ms646301(VS.85).aspx)
5151
/// </summary>
5252
/// <param name="virtualKeyCode">
53-
/// Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code.
53+
/// Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code.
5454
/// If a non-English keyboard layout is used, virtual keys with values in the range ASCII A through Z and 0 through 9 are used to specify most of the character keys. For example, for the German keyboard layout, the virtual key of value ASCII O (0x4F) refers to the "o" key, whereas VK_OEM_1 refers to the "o with umlaut" key.
5555
/// </param>
5656
/// <returns>
57-
/// The return value specifies the status of the specified virtual key, as follows:
57+
/// The return value specifies the status of the specified virtual key, as follows:
5858
/// If the high-order bit is 1, the key is down; otherwise, it is up.
5959
/// If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
6060
/// </returns>
6161
/// <remarks>
62-
/// The key status returned from this function changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.
63-
/// An application calls GetKeyState in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated.
64-
/// To retrieve state information for all the virtual keys, use the GetKeyboardState function.
65-
/// An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the nVirtKey parameter. This gives the status of the SHIFT, CTRL, or ALT keys without distinguishing between left and right. An application can also use the following virtual-key code constants as values for nVirtKey to distinguish between the left and right instances of those keys.
62+
/// The key status returned from this function changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.
63+
/// An application calls GetKeyState in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated.
64+
/// To retrieve state information for all the virtual keys, use the GetKeyboardState function.
65+
/// An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the nVirtKey parameter. This gives the status of the SHIFT, CTRL, or ALT keys without distinguishing between left and right. An application can also use the following virtual-key code constants as values for nVirtKey to distinguish between the left and right instances of those keys.
6666
/// VK_LSHIFT
6767
/// VK_RSHIFT
6868
/// VK_LCONTROL
6969
/// VK_RCONTROL
7070
/// VK_LMENU
7171
/// VK_RMENU
72-
///
73-
/// These left- and right-distinguishing constants are available to an application only through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.
72+
///
73+
/// These left- and right-distinguishing constants are available to an application only through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.
7474
/// </remarks>
7575
[DllImport("user32.dll", SetLastError = true)]
7676
public static extern short GetKeyState(ushort virtualKeyCode);
@@ -91,7 +91,7 @@ internal static class NativeMethods
9191
public static extern uint SendInput(uint numberOfInputs, Input[] inputs, int sizeOfInputStructure);
9292

9393
/// <summary>
94-
/// The <see cref="GetMessageExtraInfo"/> function retrieves the extra message information for the current thread. Extra message information is an application- or driver-defined value associated with the current thread's message queue.
94+
/// The <see cref="GetMessageExtraInfo"/> function retrieves the extra message information for the current thread. Extra message information is an application- or driver-defined value associated with the current thread's message queue.
9595
/// </summary>
9696
/// <returns></returns>
9797
/// <remarks>To set a thread's extra message information, use the SetMessageExtraInfo function. </remarks>
@@ -114,5 +114,8 @@ internal static class NativeMethods
114114
/// <returns>Returns <c>true</c> if successful or <c>false</c> otherwise.</returns>
115115
[DllImport("user32.dll", SetLastError = true)]
116116
public static extern bool GetCursorPos(out Point lpPoint);
117+
118+
[DllImport("user32.dll")]
119+
public static extern uint GetSystemMetrics(uint smIndex);
117120
}
118121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
namespace GregsStack.InputSimulatorStandard.Native
2+
{
3+
/// <summary>
4+
/// Specifies the type of the system metric. This member can be one of the following values.
5+
/// </summary>
6+
internal enum SystemMetric : uint
7+
{
8+
SM_CXSCREEN = 0,
9+
SM_CYSCREEN = 1,
10+
SM_CXVSCROLL = 2,
11+
SM_CYHSCROLL = 3,
12+
SM_CYCAPTION = 4,
13+
SM_CXBORDER = 5,
14+
SM_CYBORDER = 6,
15+
SM_CXDLGFRAME = 7,
16+
//SM_CXFIXEDFRAME = 7,
17+
SM_CYDLGFRAME = 8,
18+
//SM_CYFIXEDFRAME = 8,
19+
SM_CYVTHUMB = 9,
20+
SM_CXHTHUMB = 10,
21+
SM_CXICON = 11,
22+
SM_CYICON = 12,
23+
SM_CXCURSOR = 13,
24+
SM_CYCURSOR = 14,
25+
SM_CYMENU = 15,
26+
SM_CXFULLSCREEN = 16,
27+
SM_CYFULLSCREEN = 17,
28+
SM_CYKANJIWINDOW = 18,
29+
SM_MOUSEPRESENT = 19,
30+
SM_CYVSCROLL = 20,
31+
SM_CXHSCROLL = 21,
32+
SM_DEBUG = 22,
33+
SM_SWAPBUTTON = 23,
34+
SM_CXMIN = 28,
35+
SM_CYMIN = 29,
36+
SM_CXSIZE = 30,
37+
SM_CYSIZE = 31,
38+
//SM_CXSIZEFRAME = 32,
39+
SM_CXFRAME = 32,
40+
//SM_CYSIZEFRAME = 33,
41+
SM_CYFRAME = 33,
42+
SM_CXMINTRACK = 34,
43+
SM_CYMINTRACK = 35,
44+
SM_CXDOUBLECLK = 36,
45+
SM_CYDOUBLECLK = 37,
46+
SM_CXICONSPACING = 38,
47+
SM_CYICONSPACING = 39,
48+
SM_MENUDROPALIGNMENT = 40,
49+
SM_PENWINDOWS = 41,
50+
SM_DBCSENABLED = 42,
51+
SM_CMOUSEBUTTONS = 43,
52+
SM_SECURE = 44,
53+
SM_CXEDGE = 45,
54+
SM_CYEDGE = 46,
55+
SM_CXMINSPACING = 47,
56+
SM_CYMINSPACING = 48,
57+
SM_CXSMICON = 49,
58+
SM_CYSMICON = 50,
59+
SM_CYSMCAPTION = 51,
60+
SM_CXSMSIZE = 52,
61+
SM_CYSMSIZE = 53,
62+
SM_CXMENUSIZE = 54,
63+
SM_CYMENUSIZE = 55,
64+
SM_ARRANGE = 56,
65+
SM_CXMINIMIZED = 57,
66+
SM_CYMINIMIZED = 58,
67+
SM_CXMAXTRACK = 59,
68+
SM_CYMAXTRACK = 60,
69+
SM_CXMAXIMIZED = 61,
70+
SM_CYMAXIMIZED = 62,
71+
SM_NETWORK = 63,
72+
SM_CLEANBOOT = 67,
73+
SM_CXDRAG = 68,
74+
SM_CYDRAG = 69,
75+
SM_SHOWSOUNDS = 70,
76+
SM_CXMENUCHECK = 71,
77+
SM_CYMENUCHECK = 72,
78+
SM_SLOWMACHINE = 73,
79+
SM_MIDEASTENABLED = 74,
80+
SM_MOUSEWHEELPRESENT = 75,
81+
SM_XVIRTUALSCREEN = 76,
82+
SM_YVIRTUALSCREEN = 77,
83+
SM_CXVIRTUALSCREEN = 78,
84+
SM_CYVIRTUALSCREEN = 79,
85+
SM_CMONITORS = 80,
86+
SM_SAMEDISPLAYFORMAT = 81,
87+
SM_IMMENABLED = 82,
88+
SM_CXFOCUSBORDER = 83,
89+
SM_CYFOCUSBORDER = 84,
90+
SM_TABLETPC = 86,
91+
SM_MEDIACENTER = 87,
92+
SM_STARTER = 88,
93+
SM_SERVERR2 = 89,
94+
SM_MOUSEHORIZONTALWHEELPRESENT = 91,
95+
SM_CXPADDEDBORDER = 92,
96+
SM_DIGITIZER = 94,
97+
SM_MAXIMUMTOUCHES = 95,
98+
SM_REMOTESESSION = 0x1000,
99+
SM_SHUTTINGDOWN = 0x2000,
100+
SM_REMOTECONTROL = 0x2001
101+
}
102+
}

0 commit comments

Comments
 (0)