KeyWatch/KeyWatch++/Form1.cs

119 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
using WindowsInput;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Xml.Serialization;
using System.IO;
namespace KeyWatch__
{
public partial class Form1 : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
static bool CapsLock { get { return (((ushort)GetKeyState(0x14)) & 0xffff) != 0; } }
static bool NumLock { get { return (((ushort)GetKeyState(0x90)) & 0xffff) != 0; } }
static bool ScrollLock { get { return (((ushort)GetKeyState(0x91)) & 0xffff) != 0; } }
private static volatile bool _doingWork = false;
private string _numlockSelection;
private string _capslockSelection;
private string _scrolllockSelection;
private System.Timers.Timer _checkKeysTimer = new System.Timers.Timer();
public Form1() {
InitializeComponent();
_checkKeysTimer.Interval = 500;
_checkKeysTimer.AutoReset = true;
_checkKeysTimer.Elapsed += _checkKeysTimer_Elapsed;
_checkKeysTimer.Start();
}
private void _checkKeysTimer_Elapsed(object sender, ElapsedEventArgs e) {
InputSimulator _sim = new InputSimulator();
bool _manageNum = check_Numlock.Checked;
bool _manageCaps = check_Capslock.Checked;
bool _manageScroll = check_Scrolllock.Checked;
bool watching = _manageCaps || _manageNum || _manageScroll;
if (!watching) return; // nothing to manage, who cares
if (_doingWork) return; // already working on things, skip
_doingWork = true;
// Set desired key state
if (_manageNum) {
if (_numlockSelection != null) {
if ((_numlockSelection == "Enabled" && !NumLock) ||
(_numlockSelection == "Disabled" && NumLock)) {
_sim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.NUMLOCK);
Trace.WriteLine(String.Format("{0} - enable Numlock", DateTime.Now));
}
}
}
if (_manageCaps) {
if ((_capslockSelection == "Enabled" && !CapsLock) ||
(_capslockSelection == "Disabled" && CapsLock)) {
_sim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.CAPITAL);
Trace.WriteLine(String.Format("{0} - disable Capslock", DateTime.Now));
}
}
if (_manageScroll) {
if ((_scrolllockSelection == "Enabled" && !ScrollLock) ||
(_scrolllockSelection == "Disabled" && ScrollLock)) {
_sim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.SCROLL);
Trace.WriteLine(String.Format("{0} - disable Scrolllock", DateTime.Now));
}
}
_doingWork = false;
}
private void comboBox_numlock_SelectedIndexChanged(object sender, EventArgs e) {
_numlockSelection = (string)comboBox_numlock.SelectedItem;
Properties.KeyWatch.Default.NumlockText = _numlockSelection;
Properties.KeyWatch.Default.Save();
}
private void comboBox_capslock_SelectedIndexChanged(object sender, EventArgs e) {
_capslockSelection = (string)comboBox_capslock.SelectedItem;
Properties.KeyWatch.Default.CapslockText = _capslockSelection;
Properties.KeyWatch.Default.Save();
}
private void comboBox_scrolllock_SelectedIndexChanged(object sender, EventArgs e) {
_scrolllockSelection = (string)comboBox_scrolllock.SelectedItem;
Properties.KeyWatch.Default.ScrolllockText = _scrolllockSelection;
Properties.KeyWatch.Default.Save();
}
private void check_Numlock_CheckedChanged(object sender, EventArgs e) {
Properties.KeyWatch.Default.Numlock = check_Numlock.Checked;
Properties.KeyWatch.Default.Save();
}
private void check_Capslock_CheckedChanged(object sender, EventArgs e) {
Properties.KeyWatch.Default.Capslock = check_Capslock.Checked;
Properties.KeyWatch.Default.Save();
}
private void check_Scrolllock_CheckedChanged(object sender, EventArgs e) {
Properties.KeyWatch.Default.Scrolllock = check_Scrolllock.Checked;
Properties.KeyWatch.Default.Save();
}
}
}