-
Notifications
You must be signed in to change notification settings - Fork 84
/
keyStore.go
54 lines (48 loc) · 1011 Bytes
/
keyStore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package oak
import (
"sync"
"time"
)
var (
keyState = make(map[string]bool)
keyDurations = make(map[string]time.Time)
keyLock = sync.RWMutex{}
durationLock = sync.RWMutex{}
)
// SetUp, SetDown, and IsDown all
// control access to a keystate map
// from key strings to down or up boolean
// states.
func setUp(key string) {
keyLock.Lock()
durationLock.Lock()
delete(keyState, key)
delete(keyDurations, key)
durationLock.Unlock()
keyLock.Unlock()
}
func setDown(key string) {
keyLock.Lock()
keyState[key] = true
keyDurations[key] = time.Now()
keyLock.Unlock()
}
// IsDown returns whether a key is held down
func IsDown(key string) (k bool) {
keyLock.RLock()
k = keyState[key]
keyLock.RUnlock()
return
}
// IsHeld returns whether a key is held down, and for how long
func IsHeld(key string) (k bool, d time.Duration) {
keyLock.RLock()
k = keyState[key]
keyLock.RUnlock()
if k {
durationLock.RLock()
d = time.Since(keyDurations[key])
durationLock.RUnlock()
}
return
}