代码之家  ›  专栏  ›  技术社区  ›  Ceilingfish

响应scala中的关键事件

  •  6
  • Ceilingfish  · 技术社区  · 15 年前

    我在这里找到了一个其他人如何绕过这个问题的例子: http://houseofmirrors.googlecode.com/svn/trunk/src/src/main/scala/HouseGui.scala

    5 回复  |  直到 15 年前
        1
  •  7
  •   Mika Harju    13 年前

    这似乎适用于Scala2.9

    package fi.harjum.swing
    
    import scala.swing._
    import scala.swing.event._
    import java.awt.event._
    
    object KeyEventTest extends SimpleSwingApplication {
        def top = new MainFrame {
            val label = new Label {
                text = "No click yet"
            }
            contents = new BoxPanel(Orientation.Vertical) {
                contents += label
                border = Swing.EmptyBorder(30,30,10,10)
                listenTo(keys)
                reactions += {
                    case KeyPressed(_, Key.Space, _, _) =>
                        label.text = "Space is down"
                    case KeyReleased(_, Key.Space, _, _) =>
                        label.text = "Space is up"
                }
                focusable = true
                requestFocus
            }
        }
    }      
    
        2
  •  3
  •   Oleg    14 年前

    this.keys 如果组件是Panel或派生类,还应该调用requestFocus或设置focusable=true。

        3
  •  1
  •   Community Mohan Dere    9 年前

    this.keys (其中 this 是接收键盘事件的GUI元素)。参见关于 mouse event .

        4
  •  0
  •   Ceilingfish    15 年前

    为此,我的解决方案要求我执行以下操作:

    class MyFrame extends MainFrame {
    
    this.peer.addKeyListener(new KeyListener() {
        def keyPressed(e:KeyEvent) {
          println("key pressed")
        }
    
        def keyReleased(e:KeyEvent) {
          println("key released")
        }
    
    def keyTyped(e:KeyEvent) {
          println("key typed")
        }
     })
    
    }
    

    不过,这似乎只有在没有按钮对象连接到该组件或其任何子组件时才起作用。

        5
  •  0
  •   Adam Bergmark    14 年前

    所有组件都具有 keys 发布这些事件(所以 MainFrame 没有)。不确定什么是最好的解决方案,但总是可以将框架中的所有内容都封装在 Component 听听它的声音 钥匙

    推荐文章