You are not logged in.

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

1

Sunday, March 15th 2009, 12:43pm

how to implement mouse and keyboard events in an Applet?

Hi,

I am wondering how to implement mouse and keyboard events in an Applet?

because the interfaces MouseListener an KeyListener are not available for an Applet

any idea?

OlimilO

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

2

Sunday, March 15th 2009, 1:10pm

Mouse and Key handler sample

Hi,
I tried to implement handlers for mouse and keyboard event handlers as follows:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Option Explicit

Public Sub Applet_Click()
   trace "Applet clicked!"
End Sub

Public Sub Applet_KeyDown(KeyCode As Integer, Shift As Integer)
   trace "KeyDown: KeyCode=" & KeyCode & " Shift=" & Shift
End Sub

Public Sub Applet_KeyPress(KeyAscii As Integer)
   trace "KeyPress: KeyAscii=" & KeyAscii
End Sub

Public Sub Applet_KeyUp(KeyCode As Integer, Shift As Integer)
   trace "KeyDown: KeyCode=" & KeyCode & " Shift=" & Shift
End Sub

Public Sub Applet_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
   trace "MouseMove: Button=" & Button & " Shift=" & Shift & " X=" & X & " Y=" & Y
End Sub

Private Sub trace(msg As String)
   text1.AppendText msg & vbCrLf
   refresh
End Sub


The Click and MouseMove handlers do work as expected.

Whenever I press the ALT key during mouse movements, I get the following exception:

Quoted


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 0
at java.awt.Container.getComponent(Container.java:280)
at javax.swing.JMenuBar.getComponentAtIndex(JMenuBar.java:231)
at javax.swing.JMenuBar.getMenu(JMenuBar.java:185)
at com.sun.java.swing.plaf.windows.WindowsRootPaneUI$AltProcessor.altPressed(WindowsRootPaneUI.java:86)
at com.sun.java.swing.plaf.windows.WindowsRootPaneUI$AltProcessor.postProcessKeyEvent(WindowsRootPaneUI.java:137)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:691)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:938 )
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:810)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:645)
at java.awt.Component.dispatchEventImpl(Component.java:3857)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)



The KeyUp, KeyPress, and KeyDown handlers don't seem to work.

Greetings!

A1880

This post has been edited 1 times, last edit by "A1880" (Mar 15th 2009, 1:38pm)


Rate this thread
WoltLab Burning Board