简体   繁体   English

如何处理 Spec 2 工具栏中的事件/检测按住 SHIFT 键?

[英]How to handle the event / detect hold SHIFT key in Spec 2 toolbars?

I have a toolbar ( SpToolbarPresenter ) as an instance var toolbar and I have an #updatePresenter implementation as:我有一个工具栏( SpToolbarPresenter )作为实例 var toolbar ,我有一个#updatePresenter实现:

updatePresenter

    cmds isEmpty ifTrue: [ ^ self ].

    btnHandlers := cmds collect: [ :cmd | 
                          | btn |
                          btn := SpToolbarButtonPresenter new
                                     label: cmd name;
                                     action: [];
                                     help: cmd help.
                          toolbar addItem: btn.
                          (SpEventHandler for: btn) whenMouseDownDo: [ :ev | 
                              self onCmd: cmd name event: ev ] ]

I use SpEventHandler instead of #action: because I want to detect hold SHIFT key while the toolbar's button is clicked.我使用SpEventHandler而不是#action:因为我想在单击工具栏按钮时检测按住 SHIFT 键。 However, I still need the #action: because without it I get a strange error like "#cull: was sent to nil".但是,我仍然需要#action:因为没有它我会收到一个奇怪的错误,例如“#cull: was sent to nil”。

This code works for the second toolbar's button (I have two of them on the toolbar), but not for the first one, The first toolbar's button never gets the event, so onCmd is never called.此代码适用于第二个工具栏的按钮(我在工具栏上有两个),但不适用于第一个,第一个工具栏的按钮永远不会获得事件,因此永远不会调用onCmd

So, how to either 1) to detect hold SHIFT while toolbar's button is clicked or 2) to fix this code, so all buttons will get this event?那么,如何 1)在单击工具栏按钮时检测按住 SHIFT 或 2)修复此代码,以便所有按钮都会收到此事件? Any help will be useful.任何帮助都会很有用。

PS. PS。 Windows 10, Spec 2, Pharo-9.0.0+build.940.sha.deeec198ef752789431ee24667709a4a3ff87bda (64 Bit) Windows 10,规格 2, Pharo-9.0.0+build.940.sha.deeec198ef752789431ee24667709a4a3ff87bda (64 Bit)

as @RandomB says, but also adding a shiftPressed test:正如@RandomB 所说,还添加了一个shiftPressed测试:

updatePresenter

    commands do: [ :aCommand | 
        | button |
        button := self newToolbarButton
                   label: aCommand name;
                   action: [  ];
                   help: aCommand help.
        toolbar addItem: button.
        button eventHandler whenMouseDownDo: [ :event | 
            event shiftPressed ifTrue: [
                self onCmd: aCommand name event: event ] ] ]

also, even not being directly related, I cannot avoid to say: use complete variable/method names: it will make your code a lot more readable :)此外,即使没有直接相关,我也无法避免说:使用完整的变量/方法名称:它会使您的代码更具可读性:)

The fix may be:修复可能是:

updatePresenter

    cmds do: [ :cmd | 
        | btn |
        btn := SpToolbarButtonPresenter new
                   label: cmd name;
                   action: [  ];
                   help: cmd help.
        toolbar addItem: btn.
        btn eventHandler whenMouseDownDo: [ :ev | 
            self onCmd: cmd name event: ev ] ]

Tested - it works.经过测试 - 它有效。 The idea is to use eventHandler of the buttons then to create new handlers for them.这个想法是使用按钮的eventHandler然后为它们创建新的处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM