using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace Serein.Workbench.Services
{
delegate void KeyDownEventHandler(Key key);
delegate void KeyUpEventHandler(Key key);
///
/// 全局事件服务
///
internal interface IKeyEventService
{
event KeyDownEventHandler KeyDown;
event KeyUpEventHandler KeyUp;
///
/// 获取某个按键状态
///
///
///
bool GetKeyState(Key key);
///
/// 设置某个按键的状态
///
///
///
void SetKeyState(Key key, bool statestate);
}
///
/// 管理按键状态
///
internal class KeyEventService : IKeyEventService
{
///
/// 按键按下
///
public event KeyDownEventHandler KeyDown;
///
/// 按键松开
///
public event KeyUpEventHandler KeyUp;
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 SetKeyState(Key key, bool state)
{
if (state)
{
KeyDown?.Invoke(key);
}
else
{
KeyUp?.Invoke(key);
}
//Debug.WriteLine($"按键事件:{key} - {state}");
KeysState[(int)key] = state;
}
}
}