-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathKeyboard.cs
105 lines (79 loc) · 2.46 KB
/
Keyboard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// <copyright file="Keyboard.cs" company="Software Antics">
// Copyright (c) Software Antics. All rights reserved.
// </copyright>
namespace FinalEngine.Input.Keyboards;
using System;
using System.Collections.Generic;
internal sealed class Keyboard : IKeyboard, IDisposable
{
private readonly IKeyboardDevice device;
private readonly List<Key> keysDown;
private bool isDisposed;
private List<Key> keysDownLast;
public Keyboard(IKeyboardDevice device)
{
this.device = device ?? throw new ArgumentNullException(nameof(device));
this.keysDown = [];
this.keysDownLast = [];
this.device.KeyDown += this.Device_KeyDown;
this.device.KeyUp += this.Device_KeyUp;
}
public bool IsAltDown
{
get { return this.keysDown.Contains(Key.LeftAlt) || this.keysDown.Contains(Key.RightAlt); }
}
public bool IsCapsLocked { get; private set; }
public bool IsControlDown
{
get { return this.keysDown.Contains(Key.LeftControl) || this.keysDown.Contains(Key.RightControl); }
}
public bool IsNumLocked { get; private set; }
public bool IsShiftDown
{
get { return this.keysDown.Contains(Key.LeftShift) || this.keysDown.Contains(Key.RightShift); }
}
public void Dispose()
{
if (this.isDisposed)
{
return;
}
if (this.device != null)
{
this.device.KeyDown -= this.Device_KeyDown;
this.device.KeyUp -= this.Device_KeyUp;
}
this.isDisposed = true;
}
public bool IsKeyDown(Key key)
{
return this.keysDown.Contains(key);
}
public bool IsKeyPressed(Key key)
{
return this.keysDown.Contains(key) && !this.keysDownLast.Contains(key);
}
public bool IsKeyReleased(Key key)
{
return !this.keysDown.Contains(key) && this.keysDownLast.Contains(key);
}
public void Update()
{
this.keysDownLast = new List<Key>(this.keysDown);
}
private void Device_KeyDown(object? sender, KeyEventArgs e)
{
ArgumentNullException.ThrowIfNull(e, nameof(e));
this.IsCapsLocked = e.CapsLock;
this.IsNumLocked = e.NumLock;
this.keysDown.Add(e.Key);
}
private void Device_KeyUp(object? sender, KeyEventArgs e)
{
ArgumentNullException.ThrowIfNull(e, nameof(e));
while (this.keysDown.Contains(e.Key))
{
this.keysDown.Remove(e.Key);
}
}
}