[英]Can't peek keyboard events multiple times in Smalltalk Pharo 2
我想看一下鍵盤事件,根據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]
如果分析inst var對eventQueue的引用
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的支持
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.