检测空格键事件

发布于 2025-01-06 08:50:30 字数 205 浏览 2 评论 0 原文

空格键在许多 UI 字段上都有默认操作:单击按钮字段、选择/取消选择复选框、在垂直字段管理器上滚动。

我的屏幕有一个超过 20 行的列表字段。当用户点击空格键时,我希望列表字段滚动。

例如,黑莓默认日历应用程序,当我们按空格键时,它会向下滚动。 和黑莓默认的短信一样,当我们按空格键时,它会向下滚动。

这是默认属性吗?或者我需要为空格键编写代码吗?

The spacebar has a default action on many UI fields: click for buttonfield, select/unselect for checkbox, scrolling on a verticalfieldmanager.

My screen has a listfield with more than 20 rows. When the user hits the spacebar, I want the listfield to scroll.

For example, BlackBerry default calendar app, when we hit spacebar, it will scroll down.
and BlackBerry default text Messages, when we hit spacebar, it will scroll down.

is this a default property? or do I need to write code for spacebar key?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘香 2025-01-13 08:50:30

您必须创建一个自定义按键侦听器:

private class CustomKeyListener implements KeyListener {

    public boolean keyChar(char key, int status, int time) {
        if(key == Characters.SPACE){
            //TODO handle key here
            //WARNING: this code runs on event thread!
            return true;
        } 

        return false;
    }

    public boolean keyDown(int keycode, int time) {         
        return false;
    }

    public boolean keyRepeat(int keycode, int time) {
        return false;
    }

    public boolean keyStatus(int keycode, int time) {
        return false;
    }

    public boolean keyUp(int keycode, int time) {
        return false;
    }       
}

然后使用按键侦听器的实例作为参数调用 Mainscreen.addKeyListener

从那里,您可以使用 Manager.setVerticalScroll 方法。如果您想增加它,您可以调用 Manager.getVerticalScroll 然后添加一个固定值。如果您的屏幕中没有嵌套的 VerticalFieldManager,您可以尝试使用屏幕的默认设置,您可以通过调用 Mainscreen.getMainManager

更新:
对于列表字段,您可以调用 ListField.getSelectedIndex< code>ListField.setSelectedIndex 来更改元素,但这不是平滑的滚动。但是,如果将列表字段放置在 VerticalFieldManager 中,则可以如上所述更改管理器滚动。

You have to create a custom key listener:

private class CustomKeyListener implements KeyListener {

    public boolean keyChar(char key, int status, int time) {
        if(key == Characters.SPACE){
            //TODO handle key here
            //WARNING: this code runs on event thread!
            return true;
        } 

        return false;
    }

    public boolean keyDown(int keycode, int time) {         
        return false;
    }

    public boolean keyRepeat(int keycode, int time) {
        return false;
    }

    public boolean keyStatus(int keycode, int time) {
        return false;
    }

    public boolean keyUp(int keycode, int time) {
        return false;
    }       
}

Then call Mainscreen.addKeyListener with an instance of your key listener as parameter.

From there you can change your manager (main manager or nested one) scroll with the Manager.setVerticalScroll method. If you want to increment it, you can retrieve the current scroll calling Manager.getVerticalScroll and then add a fixed value. In case you don't have a nested VerticalFieldManager in your screen, you can try with your screen's default, which you can obtain calling Mainscreen.getMainManager.

UPDATE:
For List fields, you can call ListField.getSelectedIndex and ListField.setSelectedIndex to change elements, but this is not an smooth scrolling. But if you placed the list field inside a VerticalFieldManager, you can change the manager scroll as described above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文