using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using static System.Windows.Forms.AxHost;
namespace Serein.Workbench.Services
{
public delegate void KeyDownEventHandler(Key key);
public delegate void KeyUpEventHandler(Key key);
///
/// 全局按键事件服务
///
public interface IKeyEventService
{
event KeyDownEventHandler OnKeyDown;
event KeyUpEventHandler OnKeyUp;
///
/// 获取某个按键状态
///
///
///
bool GetKeyState(Key key);
///
/// 按下了某个键
///
///
void KeyDown(Key key);
///
/// 抬起了某个键
///
///
void KeyUp(Key key);
}
///
/// 管理按键状态
///
public class KeyEventService : IKeyEventService
{
///
/// 按键按下
///
public event KeyDownEventHandler OnKeyDown;
///
/// 按键松开
///
public event KeyUpEventHandler OnKeyUp;
public KeyEventService()
{
var arr = Enum.GetValues();
KeysState = new bool[arr.Length];
// 绑定快捷键
//HotKeyManager.SetHotKey(saveMenuItem, new KeyGesture(Key.S, KeyModifiers.Control));
}
private readonly bool[] KeysState;
public bool GetKeyState(Key key)
{
return KeysState[(int)key];
}
public void KeyDown(Key key)
{
KeysState[(int)key] = true;
OnKeyDown?.Invoke(key);
//Debug.WriteLine($"按键按下事件:{key}");
}
public void KeyUp(Key key)
{
KeysState[(int)key] = false;
OnKeyUp?.Invoke(key);
//Debug.WriteLine($"按键抬起事件:{key}");
}
}
}