- Fixed WindowsInput bugs causing the program to close on random key chords.

This commit is contained in:
David Khachaturov 2020-06-14 10:19:21 +01:00
parent ecfa890c75
commit b499db2f0a
5 changed files with 112 additions and 110 deletions

View file

@ -119,8 +119,8 @@
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="WindowsInput, Version=6.1.1.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="WindowsInput, Version=6.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\WindowsInput.6.1.1\lib\net461\WindowsInput.dll</HintPath> <HintPath>..\packages\WindowsInput.6.1.0\lib\net461\WindowsInput.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -244,10 +244,11 @@ namespace BetterJoyForCemu {
public byte LED = 0x0; public byte LED = 0x0;
public string serial_number; public string serial_number;
bool thirdParty = false;
private float[] activeData; private float[] activeData;
public Joycon(IntPtr handle_, bool imu, bool localize, float alpha, bool left, string path, string serialNum, int id = 0, bool isPro = false, bool isSnes = false) { public Joycon(IntPtr handle_, bool imu, bool localize, float alpha, bool left, string path, string serialNum, int id = 0, bool isPro = false, bool isSnes = false, bool thirdParty = false) {
serial_number = serialNum; serial_number = serialNum;
activeData = new float[6]; activeData = new float[6];
handle = handle_; handle = handle_;
@ -264,6 +265,7 @@ namespace BetterJoyForCemu {
this.isPro = isPro || isSnes; this.isPro = isPro || isSnes;
this.isSnes = isSnes; this.isSnes = isSnes;
isUSB = serialNum == "000000000001"; isUSB = serialNum == "000000000001";
thirdParty = thirdParty;
this.path = path; this.path = path;
@ -1030,7 +1032,7 @@ namespace BetterJoyForCemu {
} }
private void dump_calibration_data() { private void dump_calibration_data() {
if (isSnes) if (isSnes || thirdParty)
return; return;
HIDapi.hid_set_nonblocking(handle, 0); HIDapi.hid_set_nonblocking(handle, 0);
byte[] buf_ = ReadSPI(0x80, (isLeft ? (byte)0x12 : (byte)0x1d), 9); // get user calibration data if possible byte[] buf_ = ReadSPI(0x80, (isLeft ? (byte)0x12 : (byte)0x1d), 9); // get user calibration data if possible

View file

@ -194,7 +194,7 @@ namespace BetterJoyForCemu {
bool isPro = prod_id == product_pro; bool isPro = prod_id == product_pro;
bool isSnes = prod_id == product_snes; bool isSnes = prod_id == product_snes;
j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft, enumerate.path, enumerate.serial_number, j.Count, isPro, isSnes)); j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft, enumerate.path, enumerate.serial_number, j.Count, isPro, isSnes, thirdParty != null));
foundNew = true; foundNew = true;
j.Last().form = form; j.Last().form = form;
@ -362,8 +362,8 @@ namespace BetterJoyForCemu {
public static List<SController> thirdPartyCons = new List<SController>(); public static List<SController> thirdPartyCons = new List<SController>();
private static WindowsInput.Events.Sources.IKeyboardEventSource keyboard; private static WindowsInput.EventSources.IKeyboardEventSource keyboard;
private static WindowsInput.Events.Sources.IMouseEventSource mouse; private static WindowsInput.EventSources.IMouseEventSource mouse;
public static void Start() { public static void Start() {
pid = Process.GetCurrentProcess().Id.ToString(); // get current process id for HidCerberus.Srv pid = Process.GetCurrentProcess().Id.ToString(); // get current process id for HidCerberus.Srv
@ -446,7 +446,7 @@ namespace BetterJoyForCemu {
form.console.AppendText("All systems go\r\n"); form.console.AppendText("All systems go\r\n");
} }
private static void Mouse_MouseEvent(object sender, WindowsInput.Events.Sources.EventSourceEventArgs<WindowsInput.Events.Sources.MouseEvent> e) { private static void Mouse_MouseEvent(object sender, WindowsInput.EventSources.EventSourceEventArgs<WindowsInput.EventSources.MouseEvent> e) {
if (e.Data.ButtonDown != null) { if (e.Data.ButtonDown != null) {
string res_val = Config.Value("reset_mouse"); string res_val = Config.Value("reset_mouse");
if (res_val.StartsWith("mse_")) if (res_val.StartsWith("mse_"))
@ -469,7 +469,7 @@ namespace BetterJoyForCemu {
} }
} }
private static void Keyboard_KeyEvent(object sender, WindowsInput.Events.Sources.EventSourceEventArgs<WindowsInput.Events.Sources.KeyboardEvent> e) { private static void Keyboard_KeyEvent(object sender, WindowsInput.EventSources.EventSourceEventArgs<WindowsInput.EventSources.KeyboardEvent> e) {
if (e.Data.KeyDown != null) { if (e.Data.KeyDown != null) {
string res_val = Config.Value("reset_mouse"); string res_val = Config.Value("reset_mouse");
if (res_val.StartsWith("key_")) if (res_val.StartsWith("key_"))

View file

@ -10,123 +10,123 @@ using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace BetterJoyForCemu { namespace BetterJoyForCemu {
public partial class Reassign : Form { public partial class Reassign : Form {
private WindowsInput.Events.Sources.IKeyboardEventSource keyboard; private WindowsInput.EventSources.IKeyboardEventSource keyboard;
private WindowsInput.Events.Sources.IMouseEventSource mouse; private WindowsInput.EventSources.IMouseEventSource mouse;
ContextMenuStrip menu_joy_buttons = new ContextMenuStrip(); ContextMenuStrip menu_joy_buttons = new ContextMenuStrip();
private Control curAssignment; private Control curAssignment;
public Reassign() { public Reassign() {
InitializeComponent(); InitializeComponent();
foreach (int i in Enum.GetValues(typeof(Joycon.Button))) { foreach (int i in Enum.GetValues(typeof(Joycon.Button))) {
ToolStripMenuItem temp = new ToolStripMenuItem(Enum.GetName(typeof(Joycon.Button), i)); ToolStripMenuItem temp = new ToolStripMenuItem(Enum.GetName(typeof(Joycon.Button), i));
temp.Tag = i; temp.Tag = i;
menu_joy_buttons.Items.Add(temp); menu_joy_buttons.Items.Add(temp);
} }
menu_joy_buttons.ItemClicked += Menu_joy_buttons_ItemClicked; menu_joy_buttons.ItemClicked += Menu_joy_buttons_ItemClicked;
foreach (SplitButton c in new SplitButton[] { btn_capture, btn_home, btn_sl_l, btn_sl_r, btn_sr_l, btn_sr_r, btn_reset_mouse, btn_active_gyro }) { foreach (SplitButton c in new SplitButton[] { btn_capture, btn_home, btn_sl_l, btn_sl_r, btn_sr_l, btn_sr_r, btn_reset_mouse, btn_active_gyro }) {
c.Tag = c.Name.Substring(4); c.Tag = c.Name.Substring(4);
GetPrettyName(c); GetPrettyName(c);
tip_reassign.SetToolTip(c, "Left-click to detect input.\r\nMiddle-click to clear to default.\r\nRight-click to see more options."); tip_reassign.SetToolTip(c, "Left-click to detect input.\r\nMiddle-click to clear to default.\r\nRight-click to see more options.");
c.MouseDown += Remap; c.MouseDown += Remap;
c.Menu = menu_joy_buttons; c.Menu = menu_joy_buttons;
c.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; c.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
} }
} }
private void Menu_joy_buttons_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { private void Menu_joy_buttons_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
Control c = sender as Control; Control c = sender as Control;
ToolStripItem clickedItem = e.ClickedItem; ToolStripItem clickedItem = e.ClickedItem;
SplitButton caller = (SplitButton)c.Tag; SplitButton caller = (SplitButton)c.Tag;
Config.SetValue((string)caller.Tag, "joy_" + (clickedItem.Tag)); Config.SetValue((string)caller.Tag, "joy_" + (clickedItem.Tag));
GetPrettyName(caller); GetPrettyName(caller);
} }
private void Remap(object sender, MouseEventArgs e) { private void Remap(object sender, MouseEventArgs e) {
SplitButton c = sender as SplitButton; SplitButton c = sender as SplitButton;
switch (e.Button) { switch (e.Button) {
case MouseButtons.Left: case MouseButtons.Left:
c.Text = "..."; c.Text = "...";
curAssignment = c; curAssignment = c;
break; break;
case MouseButtons.Middle: case MouseButtons.Middle:
Config.SetValue((string)c.Tag, Config.GetDefaultValue((string)c.Tag)); Config.SetValue((string)c.Tag, Config.GetDefaultValue((string)c.Tag));
GetPrettyName(c); GetPrettyName(c);
break; break;
case MouseButtons.Right: case MouseButtons.Right:
break; break;
} }
} }
private void Reassign_Load(object sender, EventArgs e) { private void Reassign_Load(object sender, EventArgs e) {
keyboard = WindowsInput.Capture.Global.KeyboardAsync(); keyboard = WindowsInput.Capture.Global.KeyboardAsync();
keyboard.KeyEvent += Keyboard_KeyEvent; keyboard.KeyEvent += Keyboard_KeyEvent;
mouse = WindowsInput.Capture.Global.MouseAsync(); mouse = WindowsInput.Capture.Global.MouseAsync();
mouse.MouseEvent += Mouse_MouseEvent; mouse.MouseEvent += Mouse_MouseEvent;
} }
private void Mouse_MouseEvent(object sender, WindowsInput.Events.Sources.EventSourceEventArgs<WindowsInput.Events.Sources.MouseEvent> e) { private void Mouse_MouseEvent(object sender, WindowsInput.EventSources.EventSourceEventArgs<WindowsInput.EventSources.MouseEvent> e) {
if (curAssignment != null && e.Data.ButtonDown != null) { if (curAssignment != null && e.Data.ButtonDown != null) {
Config.SetValue((string)curAssignment.Tag, "mse_" + ((int)e.Data.ButtonDown.Button)); Config.SetValue((string)curAssignment.Tag, "mse_" + ((int)e.Data.ButtonDown.Button));
AsyncPrettyName(curAssignment); AsyncPrettyName(curAssignment);
curAssignment = null; curAssignment = null;
e.Next_Hook_Enabled = false; e.Next_Hook_Enabled = false;
} }
} }
private void Keyboard_KeyEvent(object sender, WindowsInput.Events.Sources.EventSourceEventArgs<WindowsInput.Events.Sources.KeyboardEvent> e) { private void Keyboard_KeyEvent(object sender, WindowsInput.EventSources.EventSourceEventArgs<WindowsInput.EventSources.KeyboardEvent> e) {
if (curAssignment != null && e.Data.KeyDown != null) { if (curAssignment != null && e.Data.KeyDown != null) {
Config.SetValue((string)curAssignment.Tag, "key_" + ((int)e.Data.KeyDown.Key)); Config.SetValue((string)curAssignment.Tag, "key_" + ((int)e.Data.KeyDown.Key));
AsyncPrettyName(curAssignment); AsyncPrettyName(curAssignment);
curAssignment = null; curAssignment = null;
e.Next_Hook_Enabled = false; e.Next_Hook_Enabled = false;
} }
} }
private void Reassign_FormClosing(object sender, FormClosingEventArgs e) { private void Reassign_FormClosing(object sender, FormClosingEventArgs e) {
keyboard.Dispose(); keyboard.Dispose();
mouse.Dispose(); mouse.Dispose();
} }
private void AsyncPrettyName(Control c) { private void AsyncPrettyName(Control c) {
if (InvokeRequired) { if (InvokeRequired) {
this.Invoke(new Action<Control>(AsyncPrettyName), new object[] { c }); this.Invoke(new Action<Control>(AsyncPrettyName), new object[] { c });
return; return;
} }
GetPrettyName(c); GetPrettyName(c);
} }
private void GetPrettyName(Control c) { private void GetPrettyName(Control c) {
string val; string val;
switch (val = Config.Value((string)c.Tag)) { switch (val = Config.Value((string)c.Tag)) {
case "0": case "0":
if (c == btn_home) if (c == btn_home)
c.Text = "Guide"; c.Text = "Guide";
else else
c.Text = ""; c.Text = "";
break; break;
default: default:
Type t = val.StartsWith("joy_") ? typeof(Joycon.Button) : (val.StartsWith("key_") ? typeof(WindowsInput.Events.KeyCode) : typeof(WindowsInput.Events.ButtonCode)); Type t = val.StartsWith("joy_") ? typeof(Joycon.Button) : (val.StartsWith("key_") ? typeof(WindowsInput.Events.KeyCode) : typeof(WindowsInput.Events.ButtonCode));
c.Text = Enum.GetName(t, Int32.Parse(val.Substring(4))); c.Text = Enum.GetName(t, Int32.Parse(val.Substring(4)));
break; break;
} }
} }
private void btn_apply_Click(object sender, EventArgs e) { private void btn_apply_Click(object sender, EventArgs e) {
Config.Save(); Config.Save();
} }
private void btn_close_Click(object sender, EventArgs e) { private void btn_close_Click(object sender, EventArgs e) {
btn_apply_Click(sender, e); btn_apply_Click(sender, e);
Close(); Close();
} }
} }
} }

View file

@ -3,5 +3,5 @@
<package id="Crc32.NET" version="1.2.0" targetFramework="net461" /> <package id="Crc32.NET" version="1.2.0" targetFramework="net461" />
<package id="JetBrains.Annotations" version="2020.1.0" targetFramework="net461" /> <package id="JetBrains.Annotations" version="2020.1.0" targetFramework="net461" />
<package id="Nefarius.ViGEm.Client" version="1.16.150" targetFramework="net461" /> <package id="Nefarius.ViGEm.Client" version="1.16.150" targetFramework="net461" />
<package id="WindowsInput" version="6.1.1" targetFramework="net461" /> <package id="WindowsInput" version="6.1.0" targetFramework="net461" />
</packages> </packages>