4

我想查看键盘事件,根据文档,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导致输出立即出现?它仅在脚本运行后出现。

4

1 回答 1

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 中失去了支持

于 2013-06-06T06:37:33.330 回答