SHildebrandt
SHildebrandt

Reputation: 316

Problems with KeyEvents

I'm wondering why the following code doesn't work:

object Main extends SimpleSwingApplication {

  val dim = new Dimension(500, 110)

  def top = new MainFrame {
    contents = new FlowPanel{
      listenTo(keys, mouse.clicks)
      reactions += {
        case MouseClicked(_,_,_,_,_) => println("Mouse clicked")
        case KeyPressed(_, Key.C, _, _) => println("C pressed")
        case KeyTyped(_, Key.C, _, _) => println("C typed")
        case KeyReleased(_, Key.C, _, _) => println("C released")
      }
    }
    size = dim
  }

}

The mouse clicks will be recognized, but the keystrokes won't. I also tried different keys or modifiers, but nothing seems to have an effect. What am I doing wrong?

(In the unlikely case that this might have something to do with the environment: I'm running this code with SBT on Windows 7)

Upvotes: 2

Views: 78

Answers (1)

Luigi Plinge
Luigi Plinge

Reputation: 51109

Working from this answer, it seems you need to include the line

focusable = true

for your FlowPanel. It should then work.

Upvotes: 2

Related Questions