From a8aa0c68a91665b1cf34bb7432b93770ec8634f6 Mon Sep 17 00:00:00 2001 From: Grzegorz Kozub Date: Sat, 28 Jun 2014 20:22:05 +0200 Subject: [PATCH] Silently quit when K290 is not connected. --- KeyboardNotFoundException.cs | 12 ++++++++++++ KeyboardService.cs | 8 ++++++-- LogitechK290FnKeySwap.csproj | 1 + Program.cs | 4 ++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 KeyboardNotFoundException.cs diff --git a/KeyboardNotFoundException.cs b/KeyboardNotFoundException.cs new file mode 100644 index 0000000..51808c6 --- /dev/null +++ b/KeyboardNotFoundException.cs @@ -0,0 +1,12 @@ +namespace GrzegorzKozub.LogitechK290FnKeySwap +{ + using System; + + internal class KeyboardNotFoundException : Exception + { + public KeyboardNotFoundException(string message) + : base(message) + { + } + } +} diff --git a/KeyboardService.cs b/KeyboardService.cs index 87dafda..d9cf804 100644 --- a/KeyboardService.cs +++ b/KeyboardService.cs @@ -32,7 +32,7 @@ private static void SetFnKeyMode(FnKeyMode mode) if (keyboard == null) { - throw new ApplicationException("Could not find Logitech K290 device. It's not connected or libusb-win32 is not installed."); + throw new KeyboardNotFoundException("Could not find Logitech K290 device. It's not connected or libusb-win32 is not installed."); } var setupPacket = new UsbSetupPacket(0x40, 2, 0x001a, (short)mode, 0); @@ -53,7 +53,11 @@ private static void SetFnKeyMode(FnKeyMode mode) } finally { - keyboard.Close(); + if (keyboard != null) + { + keyboard.Close(); + } + UsbDevice.Exit(); } } diff --git a/LogitechK290FnKeySwap.csproj b/LogitechK290FnKeySwap.csproj index 68e449c..20edc8b 100644 --- a/LogitechK290FnKeySwap.csproj +++ b/LogitechK290FnKeySwap.csproj @@ -47,6 +47,7 @@ + diff --git a/Program.cs b/Program.cs index 6fe2026..1a5d4b1 100644 --- a/Program.cs +++ b/Program.cs @@ -23,6 +23,10 @@ internal static void Main(string[] arguments) KeyboardService.SwapFnKey(); } } + catch (KeyboardNotFoundException) + { + Environment.Exit(1); + } catch (Exception exception) { MessageBox.Show(exception.Message, "Logitech K290 Fn Key Swap");