From e0736a69bca912afd5148b10d05cc2f09ea78636 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Tue, 9 Nov 2010 15:19:58 +0000 Subject: [PATCH] Added workaround for zoom-in/zoom-out keys on Microsoft Digital 3000 keyboard. These keys report 0 as a device id, but no such device exists. --- .../OpenTK/Platform/Windows/WinRawKeyboard.cs | 23 +++++++++++++++++-- Source/OpenTK/Platform/Windows/WinRawMouse.cs | 13 +++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs b/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs index a1388e11..ecf3e0df 100644 --- a/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs +++ b/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs @@ -80,6 +80,16 @@ namespace OpenTK.Platform.Windows // Discover keyboard devices: foreach (RawInputDeviceList dev in ridl) { + ContextHandle id = new ContextHandle(dev.Device); + if (rawids.ContainsKey(id)) + { + // Device already registered, mark as connected + KeyboardState state = keyboards[rawids[id]]; + state.IsConnected = true; + keyboards[rawids[id]] = state; + continue; + } + string name = GetDeviceName(dev); if (name.ToLower().Contains("root")) { @@ -136,7 +146,16 @@ namespace OpenTK.Platform.Windows { RefreshDevices(); } - keyboard = keyboards[rawids[handle]]; + + if (keyboards.Count == 0) + return false; + + // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0 + // as rin.Header.Device for the "zoom-in/zoom-out" buttons. + // That's problematic, because no device has a "0" id. + // As a workaround, we'll add those buttons to the first device (if any). + int keyboard_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0; + keyboard = keyboards[keyboard_handle]; // Generic control, shift, alt keys may be sent instead of left/right. // It seems you have to explicitly register left/right events. @@ -173,7 +192,7 @@ namespace OpenTK.Platform.Windows lock (UpdateLock) { - keyboards[rawids[handle]] = keyboard; + keyboards[keyboard_handle] = keyboard; return processed; } } diff --git a/Source/OpenTK/Platform/Windows/WinRawMouse.cs b/Source/OpenTK/Platform/Windows/WinRawMouse.cs index 637b1cba..3f028bc6 100644 --- a/Source/OpenTK/Platform/Windows/WinRawMouse.cs +++ b/Source/OpenTK/Platform/Windows/WinRawMouse.cs @@ -147,7 +147,16 @@ namespace OpenTK.Platform.Windows { RefreshDevices(); } - mouse = mice[rawids[handle]]; + + if (mice.Count == 0) + return false; + + // Note:For some reason, my Microsoft Digital 3000 keyboard reports 0 + // as rin.Header.Device for the "zoom-in/zoom-out" buttons. + // That's problematic, because no device has a "0" id. + // As a workaround, we'll add those buttons to the first device (if any). + int mouse_handle = rawids.ContainsKey(handle) ? rawids[handle] : 0; + mouse = mice[mouse_handle]; if ((raw.ButtonFlags & RawInputMouseState.LEFT_BUTTON_DOWN) != 0) mouse.EnableBit((int)MouseButton.Left); if ((raw.ButtonFlags & RawInputMouseState.LEFT_BUTTON_UP) != 0) mouse.DisableBit((int)MouseButton.Left); @@ -176,7 +185,7 @@ namespace OpenTK.Platform.Windows lock (UpdateLock) { - mice[rawids[handle]] = mouse; + mice[mouse_handle] = mouse; return true; } }