2

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)

SHildebrandt
  • 306
  • 1
  • 8
  • Just as a side note: it's not so unlikely as you might think: I had problems with swing popup cause on different OS [it behaves differently](http://stackoverflow.com/q/5736872/298389). – om-nom-nom Dec 03 '12 at 04:30
  • Maybe because a panel isn't made for keyevents? Does it work for a TextField? – user unknown Dec 03 '12 at 05:29

1 Answers1

2

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

focusable = true

for your FlowPanel. It should then work.

Community
  • 1
  • 1
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180