A game framework written with osu! in mind.
at master 52 lines 1.5 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using osu.Framework.Input.StateChanges; 5using osu.Framework.Platform; 6using osu.Framework.Statistics; 7using TKKey = osuTK.Input.Key; 8 9namespace osu.Framework.Input.Handlers.Keyboard 10{ 11 public class KeyboardHandler : InputHandler 12 { 13 public override string Description => "Keyboard"; 14 15 public override bool IsActive => true; 16 17 public override bool Initialize(GameHost host) 18 { 19 if (!base.Initialize(host)) 20 return false; 21 22 if (!(host.Window is SDL2DesktopWindow window)) 23 return false; 24 25 Enabled.BindValueChanged(e => 26 { 27 if (e.NewValue) 28 { 29 window.KeyDown += handleKeyDown; 30 window.KeyUp += handleKeyUp; 31 } 32 else 33 { 34 window.KeyDown -= handleKeyDown; 35 window.KeyUp -= handleKeyUp; 36 } 37 }, true); 38 39 return true; 40 } 41 42 private void enqueueInput(IInput input) 43 { 44 PendingInputs.Enqueue(input); 45 FrameStatistics.Increment(StatisticsCounterType.KeyEvents); 46 } 47 48 private void handleKeyDown(TKKey key) => enqueueInput(new KeyboardKeyInput(key, true)); 49 50 private void handleKeyUp(TKKey key) => enqueueInput(new KeyboardKeyInput(key, false)); 51 } 52}