Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Handle multiple keypresses in example (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner committed Aug 8, 2022
1 parent ea4e6ab commit a09a48c
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions docs/src/manual/keyevents.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ and reports the time duration between the two.
There some state handling here
because of the likely event
that your keyboard is set to "repeat" a pressed key
after some initial delay.
after some initial delay and because it is possilbe to
press multiple keys at once.
This version reports the time elapsed
between the _initial_ key press and the key release.

```julia
using Gtk

time0 = nothing
const start_times = Dict{UInt32, UInt32}()

w = GtkWindow("Key Press/Release Example")

id1 = signal_connect(w, "key-press-event") do widget, event
k = event.keyval
global time0
if isnothing(time0)
time0 = event.time # archive the initial key press time
if k keys(start_times)
start_times[k] = event.time # save the initial key press time
println("You pressed key ", k, " which is '", Char(k), "'.")
else
println("repeating key ", k)
Expand All @@ -53,8 +53,8 @@ end

id2 = signal_connect(w, "key-release-event") do widget, event
k = event.keyval
duration = event.time - time0 # key press duration in msec
start_time = pop!(start_times, k) # remove the key from the dictionary
duration = event.time - start_time # key press duration in milliseconds
println("You released key ", k, " after time ", duration, " msec.")
global time0 = nothing # revert to original state for next press
end
```

0 comments on commit a09a48c

Please sign in to comment.