我想查看键盘事件,根据Sensor的文档,我可以在不使用peekKeyboardEvent从队列中删除事件的情况下执行此操作,但是它似乎不起作用.
这有效:
"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不会导致输出立即出现?它仅在脚本运行后出现.
最佳答案 首先,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...
你可以信任评论,或者自己在代码中验证,这种方法正在消耗事件而不是偷看.
因此,这种民意调查风格似乎确实已被弃用并且在法律上失去了支持