From 39110dddb69c5be70c88d1368e486d123f258ef8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Mon, 5 Oct 2015 10:03:38 -0400 Subject: [PATCH] lib/gamnit: intro `keys` to keep track of pressed keys MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/gamnit/keys.nit | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/gamnit/keys.nit diff --git a/lib/gamnit/keys.nit b/lib/gamnit/keys.nit new file mode 100644 index 0000000..85e9e1e --- /dev/null +++ b/lib/gamnit/keys.nit @@ -0,0 +1,47 @@ +# This file is part of NIT (http://www.nitlanguage.org). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Simple service keeping track of which keys are currently pressed +# +# This service revolves around `app.pressed_keys`, a `Set` of the names of currently pressed keys. +# As a `Set`, `app.pressed_keys` can be iterated and queried with `has`. +# +# Limitations: The keys names are platform dependent. +module keys + +import mnit::input +import gamnit + +redef class App + # Currently pressed keys + var pressed_keys = new Set[String] is lazy + + # Register `event` to update `app.pressed_keys` + private fun register_key_event(event: KeyEvent) + do + var key = event.name + if event.is_down then + app.pressed_keys.add key + else if app.pressed_keys.has(key) then + app.pressed_keys.remove key + end + end + + redef fun accept_event(event) + do + if event isa KeyEvent then register_key_event event + + return super + end +end -- 1.7.9.5