-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyEventHelper.bas
More file actions
161 lines (140 loc) · 5 KB
/
Copy pathKeyEventHelper.bas
File metadata and controls
161 lines (140 loc) · 5 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=8
@EndOfDesignText@
#Event: KeyPressed(KEvt As KeyEvent)
#Event: KeyTyped(KEvt As KeyEvent)
#Event: KeyReleased(KEvt As KeyEvent)
Sub Class_Globals
Private fx As JFX
Private JO As JavaObject
Private EventMap As Map
Type EventType (Module As Object,Eventname As String, EventType As Object)
'Constants / Fields are defined here and initialized in Sub UpdateConstants
'Common supertype for all key event types.
Public ANY As JavaObject
'KEY_PRESSED and KEY_RELEASED events which do not map to a valid Unicode character use this for the keyChar value.
Public CHAR_UNDEFINED As String
'This event occurs when a key has been pressed.
Public KEY_PRESSED As JavaObject
'This event occurs when a key has been released.
Public KEY_RELEASED As JavaObject
'This event occurs when a character-generating key was typed (pressed and released).
Public KEY_TYPED As JavaObject
Private IInitialized As Boolean
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
If IInitialized Then Return
EventMap.Initialize
JO.InitializeStatic("javafx.scene.input.KeyEvent")
UpdateConstants
IInitialized = True
End Sub
Private Sub UpdateConstants
ANY = JO.GetField("ANY")
CHAR_UNDEFINED = JO.GetField("CHAR_UNDEFINED")
KEY_PRESSED = JO.GetField("KEY_PRESSED")
KEY_RELEASED = JO.GetField("KEY_RELEASED")
KEY_TYPED = JO.GetField("KEY_TYPED")
End Sub
Public Sub IsInitialized As Boolean
Return IInitialized
End Sub
'***************************************************************************************************************
'Key event subs
'***************************************************************************************************************
'Create a keyevent listener on the given Node for the given eventtype
'Node : the node on which to set the listener
'EventType : The type of the event to listen for (One of the KeyEventStatic constants, KEY_PRESSED, KEY_TYPED or KEY_RELEASED).
'EventName : the JavaObject callback sub which should have the signature :{EventName}_Event(MethodName As String,Args() as Object) As Object
'Event : Pass an event if you want to reuse an already defined event which was previously returned from a call to this sub, or Null if you are adding a new event sub
Public Sub SetKeyEvent(Node As Object, EventType As Object, Module As Object, EventName As String) As Object 'ignore
If IsInitialized = False Then
Log("KeyEventHelper is not initialized")
Return Null
End If
Dim ThisEvent As Object
Dim TypeName As String
Select EventType
Case KEY_PRESSED
TypeName = "KeyPressed"
Case KEY_RELEASED
TypeName = "KeyReleased"
Case KEY_TYPED
TypeName = "KeyTyped"
End Select
If SubExists(Module,EventName & "_" & TypeName) = False Then Return Null
Dim R As Reflector
R.Target = Node
R.AddEventFilter(TypeName,"javafx.scene.input.KeyEvent." & EventType)
Dim TTYpe As EventType
TTYpe.Initialize
TTYpe.Module = Module
TTYpe.Eventname = EventName
TTYpe.EventType = EventType
Dim L As List
L.Initialize
L = EventMap.Getdefault(Node,L)
L.Add(TTYpe)
EventMap.Put(Node,L)
Return ThisEvent
End Sub
Private Sub KeyPressed_Filter (E As Event)
If E = Null Then Return
Dim L As List = EventMap.Get(Sender)
Dim KE As KeyEvent
KE.Initialize(E)
For Each TType As EventType In L
CallSubDelayed2(TType.Module,TType.Eventname & "_KeyPressed",KE)
Next
End Sub
Private Sub KeyReleased_Filter (E As Event)
If E = Null Then Return
Dim L As List = EventMap.Get(Sender)
Dim KE As KeyEvent
KE.Initialize(E)
For Each TType As EventType In L
CallSubDelayed2(TType.Module,TType.Eventname & "_KeyReleased",KE)
Next
End Sub
Private Sub KeyTyped_Filter (E As Event)
If E = Null Then Return
Dim L As List = EventMap.Get(Sender)
Dim KE As KeyEvent
KE.Initialize(E)
For Each TType As EventType In L
CallSubDelayed2(TType.Module,TType.Eventname & "_KeyTyped",KE)
Next
End Sub
'Remove a keyevent listener from the given Node for the given eventtype
'Node : the node from which to remove the listener
'EventType : The type of the event to remove (One of the KeyEventStatic constants, KEY_PRESSED, KEY_TYPED or KEY_RELEASED).
'Event : the event returned from a previous call to SetKeyEvent
Public Sub RemoveKeyEvent(Node As JavaObject, Module As Object,EventName As String,EventType As Object, Event As Object) 'ignore
Dim TypeName As String
Select EventType
Case KEY_PRESSED
TypeName = "KeyPressed"
Case KEY_RELEASED
TypeName = "KeyReleased"
Case KEY_TYPED
TypeName = "KeyTyped"
End Select
Dim L As List
L.Initialize
L = EventMap.GetDefault(TypeName,L)
Dim i As Int = 0
For Each TType As EventType In L
If TType.Module = Module And TType.Eventname = EventName And TType.EventType = EventType Then
Node.RunMethod("removeEventHandler",Array(EventType,Event))
L.RemoveAt(I)
Exit
End If
i = i + 1
Next
If L.Size = 0 Then EventMap.Remove(TypeName)
End Sub
'***************************************************************************************************************