a background keyboard listener for .NET/C#
You can:
- bring up your program window when it has no focus
- set the shortcut keys
- Import the dll or add the source code to your project.
- Add namespace using
- Create an KeyListener instance
- Create an response function
- Bind the Keys to the response function
shortcut binding:
using System.Windows;
using com.jarvisniu.utils;
namespace WpfDemo
{
public partial class MainWindow : Window
{
KeyListener keyListener = new KeyListener();
public MainWindow()
{
InitializeComponent();
keyListener.onPress("Ctrl+R F5", onPressRefresh);
}
private void onPressRefresh()
{
this.Dispatcher.Invoke(delegate
{
label1.Content = "refresh keys pressed.";
});
}
}
}
shortcut setting:
using System.Windows;
using com.jarvisniu.utils;
namespace WpfDemo
{
public partial class MainWindow : Window
{
KeyListener keyListener = new KeyListener();
public MainWindow()
{
InitializeComponent();
keyListener.onSettingChange = onSettingChange;
keyListener.onSettingConfirm = onSettingConfirm;
}
private void button_Click(object sender, RoutedEventArgs e)
{
keyListener.startSetting();
}
private void onSettingChange(string keyString)
{
Console.WriteLine("setting change to: " + keyString);
}
private void onSettingConfirm(string keyString)
{
Console.WriteLine("setting confirm to: " + keyString);
}
}
}
There are only two pair of members:
onSettingChange: void Function(string keyString)
onSettingConfirm: void Function(string keyString)
onPress(string keys, Action response)
onRelease(string keys, Action response)
The parameter keys
is the keyboard shortcuts, not case sensitive. Here are some examples:
"F1"
- a single key"Ctrl+C"
- a combined key"Ctrl+R F5"
- multiple shortcuts
Backspace
Tab
Enter
Shift
Ctrl
orControl
Alt
orAlter
Left, Right, Up, Down
A-Z
F1
-F12
v0.1.0
- support background shortcuts
v0.2.0
- support shorcut setting
- supplement the keys