我想查看键盘事件,根据Sensor
的文档,我可以在不使用peekKeyboardEvent
从队列中删除事件的情况下执行此操作,但它似乎无效。< / p>
这有效:
"Show that a single event can be checked multiple times"
Transcript clear; show: 'Type something... '; flush.
(Delay forSeconds: 2) wait.
5 timesRepeat: [
Transcript show: (Sensor peekEvent); cr
]
输出:
Type something... #(2 48243801 5 2 8 0 0 1)
#(2 48243801 5 2 8 0 0 1)
#(2 48243801 5 2 8 0 0 1)
#(2 48243801 5 2 8 0 0 1)
#(2 48243801 5 2 8 0 0 1)
但这不是:
"Show that a single keyboard event can be checked multiple times"
Transcript clear; show: 'Type something... '; flush.
(Delay forSeconds: 2) wait.
5 timesRepeat: [
Transcript show: (Sensor peekKeyboardEvent); cr
]
输出:
Type something... #(2 48205144 97 0 0 97 0 1)
nil
nil
nil
nil
另一个问题:为什么Transcript flush
不会导致输出立即出现?它仅在脚本运行后出现。
答案 0 :(得分:2)
首先,pharo是一个快速移动的目标,因此最好分辨哪个版本。
找到答案的最佳方法是浏览代码。我将在当前的开发pharo 3.0中展示这一点 如果你浏览peekKeyboardEvent的实现者(选择它然后Alt + m),你会在InputEventSensor中找到一个版本:
peekKeyboardEvent
"Allows for use of old Sensor protocol to get at the keyboard,
as when running kbdTest or the InterpreterSimulator in Morphic"
^eventQueue findFirst: [:buf | self isKbdEvent: buf]
如果您分析对eventQueue
的inst var引用initialize
"Initialize the receiver"
super initialize.
eventQueue := WaitfreeQueue new.
...snip...
然后浏览WaitfreeQueue(选择它然后Alt + b)
findFirst: aBlock
"Note, this method only for backward compatibility. It duplicating the semantics of #nextOrNilSuchThat: completely.
Use #nextOrNilSuchThat: instead "
^ self nextOrNilSuchThat: aBlock
然后:
nextOrNilSuchThat: aBlock
"Fetch an object from queue that satisfies aBlock, skipping (but not removing) any intermediate objects.
If no object has been found, answer <nil> and leave me intact.
NOTA BENE: aBlock can contain a non-local return (^).
Found item is removed from queue .
If queue currently in the middle of extraction by other process, don't wait and return <nil> immediately"
...snip...
你可以信任评论,或者自己在代码中验证,这种方法正在消耗事件而不是偷看。
所以看来确实这种民意调查方式已被弃用并且在pharo
中失去了支持