Skip to content

Commit

Permalink
Silently quit when K290 is not connected.
Browse files Browse the repository at this point in the history
  • Loading branch information
GrzegorzKozub committed Jun 28, 2014
1 parent 90ee85f commit a8aa0c6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
12 changes: 12 additions & 0 deletions KeyboardNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace GrzegorzKozub.LogitechK290FnKeySwap
{
using System;

internal class KeyboardNotFoundException : Exception
{
public KeyboardNotFoundException(string message)
: base(message)
{
}
}
}
8 changes: 6 additions & 2 deletions KeyboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -53,7 +53,11 @@ private static void SetFnKeyMode(FnKeyMode mode)
}
finally
{
keyboard.Close();
if (keyboard != null)
{
keyboard.Close();
}

UsbDevice.Exit();
}
}
Expand Down
1 change: 1 addition & 0 deletions LogitechK290FnKeySwap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="KeyboardNotFoundException.cs" />
<Compile Include="KeyboardService.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
4 changes: 4 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit a8aa0c6

Please sign in to comment.