49 lines
		
	
	
		
			No EOL
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			No EOL
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System.Collections.Generic;
 | 
						|
using Extensions;
 | 
						|
 | 
						|
namespace Domain {
 | 
						|
	public class KeyHistory {
 | 
						|
		private readonly LimitedSizeList<int> _lastPresses = new(10);
 | 
						|
 | 
						|
		private readonly bool[] _isPressed = new bool[26];
 | 
						|
		private readonly bool[] _wasPressed = new bool[26];
 | 
						|
 | 
						|
		public void KeyPressed(int key) => _lastPresses.Add(key);
 | 
						|
 | 
						|
		public void CheckPresses() {
 | 
						|
			const int aIndex = 0x41;
 | 
						|
			const int zIndex = 0x5A;
 | 
						|
 | 
						|
			for (int i = aIndex, j = 0; i <= zIndex; i++, j++) {
 | 
						|
				_wasPressed[j] = _isPressed[j];
 | 
						|
				short state = Win32API.GetAsyncKeyState(i);
 | 
						|
 | 
						|
				if (!_wasPressed[j] && state != 0) {
 | 
						|
					_isPressed[j] = true;
 | 
						|
					KeyPressed(i);
 | 
						|
				}else if (_isPressed[j] && state == 0) {
 | 
						|
					_isPressed[j] = false;
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		public bool ContainsSequence(List<int> sequence) {
 | 
						|
			if (_lastPresses.List.Count < sequence.Count)
 | 
						|
				return false;
 | 
						|
 | 
						|
			for (int i = 0; i < _lastPresses.List.Count; i++) {
 | 
						|
				if (i >= sequence.Count)
 | 
						|
					break;
 | 
						|
 | 
						|
				int keyPressed = _lastPresses.List[_lastPresses.List.Count - 1 - i];
 | 
						|
				int sequenceKey = sequence[sequence.Count - 1 - i];
 | 
						|
 | 
						|
				if (keyPressed != sequenceKey) {
 | 
						|
					return false;
 | 
						|
				}
 | 
						|
			}
 | 
						|
 | 
						|
			return true;
 | 
						|
		}
 | 
						|
	}
 | 
						|
} |