public interface KeyListener extends EventListener {
public void keyTyped(KeyEvent e);
public void keyPressed(KeyEvent e);
public void keyReleased(KeyEvent e);
}
Event type definition:
// 定義KeyPressedCallback 為一個(gè)函數(shù)指針的類型,
// 該函數(shù)接受一個(gè)整數(shù)型參數(shù),無(wú)返回值
typedef void (*KeyPressedCallback)(int keyCode);
Publisher:
class Publisher
{
public KeyPressedCallback KeyPressedSink = null;
...
void FireEvent(int KeyCode)
{
if (KeyPressedSink != null)
(*KeyPressedSink)(keyCode);//callback
}
}
Subscriber:
void KeyPressedHandler(int keyCode)
{
...
}
...
Publisher publisher = new Publisher();
//reGISter
publisher.KeyPressedSink = &KeyPressedHandler; Event type definition:
// 定義KeyPressedDelegate 為一個(gè)類似函數(shù)指針的類型,
// 該函數(shù)接受一個(gè)整數(shù)型參數(shù),無(wú)返回值
delegate void KeyPressedDelegate(int keyCode);
Publisher:
class Publisher
{
public KeyPressedDelegate KeyPressed = null;
...
void FireEvent(int KeyCode)
{
if (KeyPressed != null)
KeyPressed(keyCode);
}
}
Subscriber:
void KeyPressedHandler(int keyCode)
{
...
}
...
Publisher publisher = new Publisher();
//register
publisher.KeyPressed = KeyPressedHandler; 新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注