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
    "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 に答える