Keyboard Reading

This commit is contained in:
Serge
2023-05-20 20:50:58 +02:00
parent 1ac0f2be08
commit 6adb2e2fcf
7 changed files with 185 additions and 33 deletions

View File

@@ -52,6 +52,8 @@ namespace GHelper
public static class AsusUSB public static class AsusUSB
{ {
public const byte HID_ID = 0x5a;
static byte[] MESSAGE_SET = { 0x5d, 0xb5, 0, 0, 0 }; static byte[] MESSAGE_SET = { 0x5d, 0xb5, 0, 0, 0 };
static byte[] MESSAGE_APPLY = { 0x5d, 0xb4 }; static byte[] MESSAGE_APPLY = { 0x5d, 0xb4 };
@@ -172,6 +174,51 @@ namespace GHelper
yield return device; yield return device;
} }
private static HidDevice? GetInputDevice()
{
HidDevice[] HidDeviceList = HidDevices.Enumerate(0x0b05, deviceIds).ToArray();
foreach (HidDevice device in HidDeviceList)
if (device.ReadFeatureData(out byte[] data, HID_ID))
return device;
return null;
}
public static void TouchpadToggle()
{
HidDevice? input = GetInputDevice();
if (input != null) input.WriteFeatureData(new byte[] { HID_ID,0xf4,0x6b});
}
public static void RunListener(Action<int> KeyHandler)
{
HidDevice? input = GetInputDevice();
if (input == null) return;
Logger.WriteLine("Input Events " + input.DevicePath);
Task.Run(() =>
{
try
{
while (true)
{
var data = input.Read().Data;
if (data.Length > 1 && data[0] == HID_ID && data[1] > 0)
{
Logger.WriteLine("Key:" + data[1]);
KeyHandler(data[1]);
}
}
}
catch (Exception ex)
{
Logger.WriteLine(ex.ToString());
}
});
}
public static byte[] AuraMessage(int mode, Color color, Color color2, int speed) public static byte[] AuraMessage(int mode, Color color, Color color2, int speed)
{ {
@@ -300,10 +347,11 @@ namespace GHelper
byte[] msg = AuraMessage(Mode, Color1, Color2, _speed); byte[] msg = AuraMessage(Mode, Color1, Color2, _speed);
var devices = GetHidDevices(deviceIds); var devices = GetHidDevices(deviceIds);
if (devices.Count() == 0) if (devices.Count() == 0)
{ {
Logger.WriteLine("USB-KB : not found"); Logger.WriteLine("USB-KB : not found");
GetHidDevices(deviceIds, 0); devices = GetHidDevices(deviceIds, 1);
} }
foreach (HidDevice device in devices) foreach (HidDevice device in devices)

View File

@@ -4,7 +4,7 @@ using System.Runtime.InteropServices;
namespace OSD namespace OSD
{ {
class OSDNativeForm : NativeWindow, IDisposable public class OSDNativeForm : NativeWindow, IDisposable
{ {
private bool _disposed = false; private bool _disposed = false;

View File

@@ -1,5 +1,6 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Text; using System.Text;
using System.Diagnostics;
namespace GHelper namespace GHelper
{ {
@@ -33,6 +34,11 @@ namespace GHelper
File.WriteAllText(path, fileContents, Encoding.Unicode); File.WriteAllText(path, fileContents, Encoding.Unicode);
} }
} }
public static bool IsRunning()
{
return (Process.GetProcessesByName("AsusOptimization").Count() > 0);
}
} }
} }

View File

@@ -1,4 +1,6 @@
using Microsoft.Win32; using Microsoft.Win32;
using NAudio.CoreAudioApi;
using OSD;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Management; using System.Management;
@@ -22,7 +24,6 @@ namespace GHelper
public static AppConfig config = new AppConfig(); public static AppConfig config = new AppConfig();
public static SettingsForm settingsForm = new SettingsForm(); public static SettingsForm settingsForm = new SettingsForm();
public static ToastForm toast = new ToastForm();
public static IntPtr unRegPowerNotify; public static IntPtr unRegPowerNotify;
@@ -30,6 +31,8 @@ namespace GHelper
private static long lastTheme; private static long lastTheme;
private static long lastAdmin; private static long lastAdmin;
private static bool isOptimizationRunning = OptimizationService.IsRunning();
private static PowerLineStatus isPlugged = PowerLineStatus.Unknown; private static PowerLineStatus isPlugged = PowerLineStatus.Unknown;
// The main entry point for the application // The main entry point for the application
@@ -103,7 +106,7 @@ namespace GHelper
SettingsToggle(action); SettingsToggle(action);
} }
//Task.Run(wmi.RunListener); if (!isOptimizationRunning) AsusUSB.RunListener(HandleEvent);
Application.Run(); Application.Run();
@@ -220,6 +223,8 @@ namespace GHelper
action = "ghelper"; action = "ghelper";
if (name == "fnf4") if (name == "fnf4")
action = "aura"; action = "aura";
if (name == "m3" && !isOptimizationRunning)
action = "micmute";
} }
switch (action) switch (action)
@@ -254,6 +259,16 @@ namespace GHelper
case "custom": case "custom":
CustomKey(name); CustomKey(name);
break; break;
case "micmute":
using (var enumerator = new MMDeviceEnumerator())
{
var commDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Communications);
bool muteStatus = !commDevice.AudioEndpointVolume.Mute;
commDevice.AudioEndpointVolume.Mute = muteStatus;
settingsForm.BeginInvoke(settingsForm.RunToast, muteStatus ? "Mic Muted" : "Mic Unmuted");
}
break;
default: default:
break; break;
} }
@@ -272,21 +287,12 @@ namespace GHelper
Logger.WriteLine("Tablet: " + tabletState + " Touchpad: " + touchpadState); Logger.WriteLine("Tablet: " + tabletState + " Touchpad: " + touchpadState);
if ((tabletState && touchpadState) || (!tabletState && !touchpadState)) if ((tabletState && touchpadState) || (!tabletState && !touchpadState)) AsusUSB.TouchpadToggle();
acpi.DeviceSet(AsusACPI.UniversalControl, AsusACPI.Touchpad_Toggle, "Touchpad");
} }
static void WatcherEventArrived(object sender, EventArrivedEventArgs e) static void HandleEvent(int EventID)
{ {
var collection = (ManagementEventWatcher)sender;
if (e.NewEvent is null) return;
int EventID = int.Parse(e.NewEvent["EventID"].ToString());
Logger.WriteLine("WMI event " + EventID);
switch (EventID) switch (EventID)
{ {
case 124: // M3 case 124: // M3
@@ -304,10 +310,52 @@ namespace GHelper
case 189: // Tablet mode case 189: // Tablet mode
TabletMode(); TabletMode();
return; return;
} }
if (isOptimizationRunning) return;
// Asus Optimization service Events
int brightness = config.getConfig("keyboard_brightness");
switch (EventID)
{
case 197: // FN+F2
brightness = Math.Max(0, brightness - 1);
config.setConfig("keyboard_brightness", brightness);
AsusUSB.ApplyBrightness(brightness);
settingsForm.BeginInvoke(settingsForm.RunToast, "Backlight -");
break;
case 196: // FN+F3
brightness = Math.Min(3, brightness + 1);
config.setConfig("keyboard_brightness", brightness);
AsusUSB.ApplyBrightness(brightness);
settingsForm.BeginInvoke(settingsForm.RunToast, "Backlight +");
break;
case 16: // FN+F7
ScreenBrightness.Adjust(-10);
settingsForm.BeginInvoke(settingsForm.RunToast, "Brightness -");
break;
case 32: // FN+F8
ScreenBrightness.Adjust(+10);
settingsForm.BeginInvoke(settingsForm.RunToast, "Brightness +");
break;
case 107: // FN+F10
AsusUSB.TouchpadToggle();
settingsForm.BeginInvoke(settingsForm.RunToast, "Touchpad");
break;
case 108: // FN+F11
Application.SetSuspendState(PowerState.Suspend, true, true);
break;
}
}
static void WatcherEventArrived(object sender, EventArrivedEventArgs e)
{
if (e.NewEvent is null) return;
int EventID = int.Parse(e.NewEvent["EventID"].ToString());
Logger.WriteLine("WMI event " + EventID);
HandleEvent(EventID);
} }
static void SettingsToggle(string action = "") static void SettingsToggle(string action = "")

46
app/ScreenBrightness.cs Normal file
View File

@@ -0,0 +1,46 @@
namespace GHelper
{
using System;
using System.Diagnostics;
using System.Management;
public static class ScreenBrightness
{
public static int Get()
{
using var mclass = new ManagementClass("WmiMonitorBrightness")
{
Scope = new ManagementScope(@"\\.\root\wmi")
};
using var instances = mclass.GetInstances();
foreach (ManagementObject instance in instances)
{
return (byte)instance.GetPropertyValue("CurrentBrightness");
}
return 0;
}
public static void Set(int brightness)
{
using var mclass = new ManagementClass("WmiMonitorBrightnessMethods")
{
Scope = new ManagementScope(@"\\.\root\wmi")
};
using var instances = mclass.GetInstances();
var args = new object[] { 1, brightness };
foreach (ManagementObject instance in instances)
{
instance.InvokeMethod("WmiSetBrightness", args);
}
}
public static void Adjust(int delta)
{
int brightness = Get();
Debug.WriteLine(brightness);
brightness = Math.Min(100, Math.Max(0, brightness + delta));
Set(brightness);
}
}
}

View File

@@ -1,12 +1,7 @@
using CustomControls; using CustomControls;
using GHelper.AnimeMatrix; using GHelper.AnimeMatrix;
using GHelper.Gpu; using GHelper.Gpu;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using Starlight.AnimeMatrix;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
@@ -22,6 +17,8 @@ namespace GHelper
private ContextMenuStrip contextMenuStrip = new CustomContextMenu(); private ContextMenuStrip contextMenuStrip = new CustomContextMenu();
private ToolStripMenuItem menuSilent, menuBalanced, menuTurbo, menuEco, menuStandard, menuUltimate, menuOptimized; private ToolStripMenuItem menuSilent, menuBalanced, menuTurbo, menuEco, menuStandard, menuUltimate, menuOptimized;
public ToastForm toast = new ToastForm();
public static System.Timers.Timer aTimer = default!; public static System.Timers.Timer aTimer = default!;
public static Point trayPoint; public static Point trayPoint;
@@ -30,7 +27,6 @@ namespace GHelper
public string perfName = "Balanced"; public string perfName = "Balanced";
public AniMatrix matrix; public AniMatrix matrix;
public Fans fans; public Fans fans;
public Extra keyb; public Extra keyb;
@@ -42,7 +38,6 @@ namespace GHelper
public SettingsForm() public SettingsForm()
{ {
InitializeComponent(); InitializeComponent();
InitTheme(true); InitTheme(true);
@@ -157,6 +152,11 @@ namespace GHelper
} }
public void RunToast(string text)
{
toast.RunToast(text);
}
public void SetContextMenu() public void SetContextMenu()
{ {
@@ -1145,7 +1145,7 @@ namespace GHelper
{ {
try try
{ {
Program.toast.RunToast(perfName); toast.RunToast(perfName);
} }
catch catch
{ {

View File

@@ -1,4 +1,5 @@
using System.Drawing.Drawing2D; using System.Diagnostics;
using System.Drawing.Drawing2D;
using OSD; using OSD;
@@ -41,12 +42,19 @@ namespace GHelper
} }
} }
class ToastForm : OSDNativeForm public class ToastForm : OSDNativeForm
{ {
protected static string toastText = "Balanced"; protected static string toastText = "Balanced";
protected static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); protected static System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public ToastForm()
{
timer.Tick += timer_Tick;
timer.Enabled = false;
timer.Interval = 2000;
}
protected override void PerformPaint(PaintEventArgs e) protected override void PerformPaint(PaintEventArgs e)
{ {
Brush brush = new SolidBrush(Color.FromArgb(150,Color.Black)); Brush brush = new SolidBrush(Color.FromArgb(150,Color.Black));
@@ -65,6 +73,7 @@ namespace GHelper
public void RunToast(string text) public void RunToast(string text)
{ {
Hide();
toastText = text; toastText = text;
Screen screen1 = Screen.FromHandle(base.Handle); Screen screen1 = Screen.FromHandle(base.Handle);
@@ -76,18 +85,13 @@ namespace GHelper
Show(); Show();
timer.Stop();
timer.Tick -= timer_Tick;
timer.Tick += timer_Tick;
timer.Enabled = true; timer.Enabled = true;
timer.Interval = 2000;
timer.Start();
} }
private void timer_Tick(object? sender, EventArgs e) private void timer_Tick(object? sender, EventArgs e)
{ {
timer.Stop(); Debug.WriteLine("Toast end");
timer.Enabled = false;
Hide(); Hide();
} }
} }