I am developing an app in C# Winforms .Net Framework and am using this global mouse hook code:
internal class GlobalMouseHook
{
#region events
// declare hook events:
public static event Action MouseMove_Event;
public static event Action LeftDown_Event;
public static event Action RightDown_Event;
public static event Action LeftUp_Event;
public static event Action RightUp_Event;
public static event Action MidDown_Event;
public static event Action MidUp_Event;
#endregion
#region constants
// declare constants:
private const int WH_MOUSE_LL = 14;
private const int WM_MOUSEMOVE = 0x0200;
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_LBUTTONUP = 0x0202;
private const int WM_RBUTTONDOWN = 0x204;
private const int WM_RBUTTONUP = 0x205;
private const int WM_MBUTTONDOWN = 0x207;
private const int WM_MBUTTONUP = 0x208;
#endregion
public static bool mouseHookActive = false;
private static IntPtr mouseHookid;
private static MouseDelegate mouseDelegate = MouseHookCallback;
private delegate IntPtr MouseDelegate(int nCode, IntPtr wParam, IntPtr lParam);
private static ProcessModule curModule = Process.GetCurrentProcess().MainModule;
private static IntPtr appHandle = GetModuleHandle(curModule.ModuleName);
public static void StartMouseHook() { mouseHookActive = true; mouseHookid = SetWindowsHookEx(WH_MOUSE_LL, mouseDelegate, appHandle, 0); }
public static void StopMouseHook() { mouseHookActive = false; UnhookWindowsHookEx(mouseHookid); }
private static IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
// detect hook trigger then select message type and execute action
if (nCode >= 0)
{
using (frmFileToy frm = new frmFileToy())
{
switch ((int)wParam)
{
case WM_MOUSEMOVE: MouseMove_Event?.Invoke(); break;
case WM_LBUTTONDOWN: LeftDown_Event?.Invoke(); break;
case WM_LBUTTONUP: LeftUp_Event?.Invoke(); break;
case WM_RBUTTONDOWN: RightDown_Event?.Invoke(); break;
case WM_RBUTTONUP: RightUp_Event?.Invoke(); break;
case WM_MBUTTONDOWN: MidDown_Event?.Invoke(); break;
case WM_MBUTTONUP: MidUp_Event?.Invoke(); break;
}
}
}
// continue to next hook
return CallNextHookEx(mouseHookid, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, MouseDelegate lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)][return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName);
}
This code works correctly when using "en-US". However, when using "de-DE" this code results in extremely laggy and jerky mouse movement for a specific use scenario.
The use scenario is specifically for a drag and drop action that starts within my application and ends outside my application with a file drop on the desktop or in another application. The purpose of the mouse hook is to detect the release of the mouse button when the file drop occurs.
There are no issues with mouse movement within my application window for any language. The laggy mouse movement begins as soon as the mouse leaves my application window - which is when the mouse hook is started. There are no language specific code items employed at any point in my code other than a dictionary of language strings which functions correctly for both "en-US" and "de-DE".
The language and culture info are assigned before "InitializeComponent();" is called with these statements:
using (RegistryKey key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\" + appName))
{
lang = (string)key.GetValue("language", lang);
}
CultureInfo ci = new CultureInfo(lang);
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = ci;
Any thoughts on how or why the language would affect the global mouse hook would be gratefully appreciated.