gpt4 book ai didi

smalltalk - 无法在 Smalltalk Pharo 2 中多次查看键盘事件

转载 作者:行者123 更新时间:2023-12-03 06:53:30 25 4
gpt4 key购买 nike

我想查看键盘事件,根据 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

进一步的问题:为什么 Transcriptlush 不会导致输出立即出现?它仅在脚本运行后出现。

最佳答案

首先,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 的支持

关于smalltalk - 无法在 Smalltalk Pharo 2 中多次查看键盘事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16395418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com