Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcut #367

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions TabletDriverGUI/ButtonMapping.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Window x:Class="TabletDriverGUI.ButtonMapping"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TabletDriverGUI"
mc:Ignorable="d"
Title="ButtonMapping" Height="380" Width="350" ResizeMode="NoResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20*"/>
<RowDefinition Height="65*"/>
<RowDefinition Height="15*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Vertical" Margin="12">
<CheckBox Name="CheckBoxDisablePenButtons" Content="Disable pen buttons"
Checked="CheckBoxDisablePenButtons_Checked" Unchecked="CheckBoxDisablePenButtons_Unchecked"/>
<CheckBox Name="CheckBoxDisableTabletButtons" Margin="0,7,0,0"
Content="Disable tablet buttons" Checked="CheckBoxDisableTabletButtons_Checked" Unchecked="CheckBoxDisableTabletButtons_Unchecked"/>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Name="PenButtonsContainer" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<GroupBox Header="Pen tip" VerticalAlignment="Top" HorizontalAlignment="Left" Width="90">
<ComboBox Name="PenTipComboBox" />
</GroupBox>
<GroupBox Header="Pen bottom" VerticalAlignment="Top" HorizontalAlignment="Left" Width="90">
<ComboBox Name="PenBottomComboBox" />
</GroupBox>
<GroupBox Header="Pen top" VerticalAlignment="Top" HorizontalAlignment="Left" Width="90">
<ComboBox Name="PenTopComboBox" />
</GroupBox>
</StackPanel>
<StackPanel Name="TabletButtonsContainer" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Name="ButtonSet" Content="Set" Width="60" Height="30" Click="ButtonSet_Click"/>
<Button Name="ButtonCancel" Margin="35,0,0,0" Width="60" Height="30" Content="Cancel" Click="ButtonCancel_Click" />
</StackPanel>
</Grid>
</Window>
272 changes: 272 additions & 0 deletions TabletDriverGUI/ButtonMapping.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TabletDriverGUI
{
public enum ButtonActionEnum : uint
{
DISABLED = 0,
MOUSE1 = 1,
MOUSE2 = 2,
MOUSE3 = 3,
MOUSE4 = 4,
MOUSE5 = 5,
SHORTCUT = 6
};

/// <summary>
/// Interaction logic for ButtonMapping.xaml
/// </summary>
public partial class ButtonMapping : Window
{
private Dictionary<string, uint> NbTabletButtonsMap;
public Dictionary<int, int[]> MacroButtonMap;

public ButtonMapping(Configuration config, string tabletName)
{
NbTabletButtonsMap = new Dictionary<string, uint>()
{
{ "Wacom CTL-470", 0 },
{ "Wacom CTL-471", 0 },
{ "Wacom CTL-472", 0 },
{ "Wacom CTL-480", 4 },
{ "Wacom CTH-480", 4 },
{ "Wacom CTL-490", 4 },
{ "XP Pen G430", 0 },
{ "XP Pen G640", 0 },
{ "Huion 420", 0 },
{ "Huion H640P", 6 },
{ "Gaomon S56K", 0 },
};
MacroButtonMap = new Dictionary<int, int[]>();

if (tabletName != null && NbTabletButtonsMap.ContainsKey(tabletName))
{
InitializeComponent();

PenTipComboBox.DropDownClosed += new EventHandler(PromptShortcutWindowEvent);
PenBottomComboBox.DropDownClosed += new EventHandler(PromptShortcutWindowEvent);
PenTopComboBox.DropDownClosed += new EventHandler(PromptShortcutWindowEvent);

for (var i = 0; i < NbTabletButtonsMap[tabletName]; ++i)
{
ComboBox cb = new ComboBox
{
Name = "TabletButton" + (i + 1).ToString()
};
cb.DropDownClosed += new EventHandler(PromptShortcutWindowEvent);

GroupBox gb = new GroupBox
{
Header = "Tablet button " + (i + 1).ToString(),
Width = 110,
Margin = new Thickness(10, 0, 0, 0),
Content = cb
};
TabletButtonsContainer.Children.Add(gb);
}

// Create button map combobox items
var tabletButtons = TabletButtonsContainer.Children;

PenTipComboBox.Items.Add("Disabled");
PenBottomComboBox.Items.Add("Disabled");
PenTopComboBox.Items.Add("Disabled");
for (var j = 0; j < tabletButtons.Count; ++j)
{
var gb = tabletButtons[j] as GroupBox;
var cb = gb.Content as ComboBox;
cb.Items.Add("Disabled");
cb.SelectedIndex = 0;
}
for (int i = 1; i <= 5; ++i)
{
PenTipComboBox.Items.Add("Mouse " + i);
PenBottomComboBox.Items.Add("Mouse " + i);
PenTopComboBox.Items.Add("Mouse " + i);
for (var j = 0; j < tabletButtons.Count; ++j)
{
var gb = tabletButtons[j] as GroupBox;
var cb = gb.Content as ComboBox;
cb.Items.Add("Mouse " + i);
}
}
PenTipComboBox.Items.Add("Shortcut");
PenBottomComboBox.Items.Add("Shortcut");
PenTopComboBox.Items.Add("Shortcut");
for (var i = 0; i < tabletButtons.Count; ++i)
{
var gb = tabletButtons[i] as GroupBox;
var cb = gb.Content as ComboBox;
cb.Items.Add("Shortcut");
}

PenTipComboBox.SelectedIndex = 0;
PenBottomComboBox.SelectedIndex = 0;
PenTopComboBox.SelectedIndex = 0;


//
// Buttons
//
PenTipComboBox.SelectedIndex = config.ButtonMap[0];
PenBottomComboBox.SelectedIndex = config.ButtonMap[1];
PenTopComboBox.SelectedIndex = config.ButtonMap[2];

for (var i = 0; i < NbTabletButtonsMap[tabletName]; ++i)
{
for (var j = 0; j < TabletButtonsContainer.Children.Count && j + 3 < config.ButtonMap.Length; ++j)
{
var gb = TabletButtonsContainer.Children[j] as GroupBox;
var cb = gb.Content as ComboBox;

cb.SelectedIndex = config.ButtonMap[3 + j];
}
}

CheckBoxDisablePenButtons.IsChecked = config.DisablePenButtons;
CheckBoxDisableTabletButtons.IsChecked = config.DisableTabletButtons;
}
else
{
throw new TabletNotRecognizedException("Tablet not recognized");
}
}

private void ButtonSet_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}

private void ButtonCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}

private void CheckBoxDisablePenButtons_Checked(object sender, RoutedEventArgs e)
{
PenButtonsContainer.IsEnabled = false;
}

private void CheckBoxDisablePenButtons_Unchecked(object sender, RoutedEventArgs e)
{
PenButtonsContainer.IsEnabled = true;
}

private void CheckBoxDisableTabletButtons_Checked(object sender, RoutedEventArgs e)
{
TabletButtonsContainer.IsEnabled = false;
}

private void CheckBoxDisableTabletButtons_Unchecked(object sender, RoutedEventArgs e)
{
TabletButtonsContainer.IsEnabled = true;
}

private enum MapType : uint
{
MAPVK_VK_TO_VSC = 0x0,
MAPVK_VSC_TO_VK = 0x1,
MAPVK_VK_TO_CHAR = 0x2,
MAPVK_VSC_TO_VK_EX = 0x3,
}

[DllImport("user32.dll")]
private static extern int ToUnicode(
uint wVirtKey,
uint wScanCode,
byte[] lpKeyState,
[Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)]
StringBuilder pwszBuff,
int cchBuff,
uint wFlags);

[DllImport("user32.dll")]
private static extern bool GetKeyboardState(byte[] lpKeyState);

[DllImport("user32.dll")]
private static extern uint MapVirtualKey(uint uCode, MapType uMapType);

private int GetVKFromModifierKey(ModifierKeys modifierKey)
{
int ch = 0;

switch (modifierKey)
{
case ModifierKeys.Control:
ch = 0x11;
break;
case ModifierKeys.Shift:
ch = 0x10;
break;
case ModifierKeys.Alt:
ch = 0x12;
break;
case ModifierKeys.Windows:
ch = 0x5B;
break;
}

return (ch);
}

private int GetVKFromKey(Key key)
{
return (KeyInterop.VirtualKeyFromKey(key));
}

private void PromptShortcutWindowEvent(object sender, EventArgs e)
{
PromptShortcutWindow(sender);
}

private void PromptShortcutWindow(object sender)
{
ComboBox cb = sender as ComboBox;

if (cb.SelectedIndex == (uint)ButtonActionEnum.SHORTCUT)
{
ShortcutMapWindow shortcutMapWindow = new ShortcutMapWindow();

shortcutMapWindow.ShowDialog();

if (shortcutMapWindow.DialogResult == true)
{
int idx = -1;
if (cb.Name == "PenTipComboBox")
idx = 0;
else if (cb.Name == "PenBottomComboBox")
idx = 1;
else if (cb.Name == "PenTopComboBox")
idx = 2;
else
idx = Int32.Parse(cb.Name.Remove(0, 12)) + 2;
List<int> l = new List<int>();

for (var i = 0; i < shortcutMapWindow.ModifierKey.Count; ++i)
l.Add(GetVKFromModifierKey(shortcutMapWindow.ModifierKey[i]));
l.Add(GetVKFromKey(shortcutMapWindow.PressedKey));
if (shortcutMapWindow.PressedKey == Key.None)
cb.SelectedIndex = (int)ButtonActionEnum.DISABLED;
MacroButtonMap[idx] = l.ToArray();
}
else
cb.SelectedIndex = (int)ButtonActionEnum.DISABLED;

shortcutMapWindow.Close();
}
}
}
}
11 changes: 8 additions & 3 deletions TabletDriverGUI/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace TabletDriverGUI
{
Expand Down Expand Up @@ -36,7 +37,9 @@ public enum OutputModes
[XmlArray("ButtonMap")]
[XmlArrayItem("Button")]
public int[] ButtonMap;
public bool DisableButtons;
public MacroButton[] MacroButtonMap;
public bool DisablePenButtons;
public bool DisableTabletButtons;

[XmlArray("CommandsAfter")]
[XmlArrayItem("Command")]
Expand Down Expand Up @@ -75,8 +78,10 @@ public Configuration()
DesktopSize = new Area(0, 0, 0, 0);
AutomaticDesktopSize = true;

ButtonMap = new int[] { 1, 2, 3 };
DisableButtons = false;
ButtonMap = new int[] { 1, 2, 3, 0, 0, 0, 0, 0, 0 };
MacroButtonMap = new MacroButton[] { };
DisablePenButtons = false;
DisableTabletButtons = false;

SmoothingEnabled = false;
SmoothingLatency = 0;
Expand Down
26 changes: 26 additions & 0 deletions TabletDriverGUI/MacroButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace TabletDriverGUI
{
//[XmlType("MacroButton")]
public class MacroButton
{
public int Index;
[XmlArray("MacroKeys")]
[XmlArrayItem("Key")]
public int[] MacroKeys;

public MacroButton() { }

public MacroButton(int index, int[] macroKeys)
{
Index = index;
MacroKeys = macroKeys;
}
}
}
Loading