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.
redef class App
redef fun accept_event(event)
do
# First, pass the event to `super`, `pressed_keys` must see all
# events but it doesn't intercept any of them.
if super then return true
return false
end
redef fun update(dt)
do
for key in pressed_keys do
if k == "left" or k == "a" then
# Act on key pressed down
print "left or a is pressed down"
end
end
end
end
Serializable::inspect
to show more useful information
more_collections :: more_collections
Highly specific, but useful, collections-related classes.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatscore :: union_find
union–find algorithm using an efficient disjoint-set data structureEulerCamera
and App::frame_core_draw
to get a stereoscopic view
# 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.
#
# ~~~nitish
# redef class App
# redef fun accept_event(event)
# do
# # First, pass the event to `super`, `pressed_keys` must see all
# # events but it doesn't intercept any of them.
# if super then return true
# return false
# end
#
# redef fun update(dt)
# do
# for key in pressed_keys do
# if k == "left" or k == "a" then
# # Act on key pressed down
# print "left or a is pressed down"
# end
# end
# end
# end
# ~~~
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
lib/gamnit/keys.nit:15,1--69,3