如何在scrollWheel事件中获取“计数”?

时间:2019-10-14 12:26:51

标签: swift macos nsscrollview

当重写scrollWheel()方法时,我会在event.description中看到一个“计数:”,当我在触控板上连续滚动时,该计数就会增加。

override func scrollWheel( with event: NSEvent )
{
    print( event )
} 

滚动时的输出:

NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.7 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-1.000000 deltaY=0.000000 count:0 phase=Began momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.7 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-8.000000 deltaY=1.000000 count:0 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.7 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-13.000000 deltaY=1.000000 count:0 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.7 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-18.000000 deltaY=2.000000 count:0 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.7 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-24.000000 deltaY=2.000000 count:0 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.7 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-92.000000 deltaY=5.000000 count:1 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.8 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-47.000000 deltaY=2.000000 count:1 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.8 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-54.000000 deltaY=3.000000 count:1 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.8 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-84.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Began
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.8 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-240.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.9 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-73.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.9 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-69.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.9 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-65.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.9 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-62.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.9 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-58.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8103.9 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-54.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.0 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-51.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.0 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-47.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.0 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-43.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.0 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-39.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Changed
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.0 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=0.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Ended
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.0 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-11.000000 deltaY=0.000000 count:2 phase=Began momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.0 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-56.000000 deltaY=2.000000 count:2 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.1 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-61.000000 deltaY=3.000000 count:2 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.1 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-82.000000 deltaY=4.000000 count:2 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.1 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-106.000000 deltaY=6.000000 count:2 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.1 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-110.000000 deltaY=7.000000 count:2 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.1 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-68.000000 deltaY=5.000000 count:2 phase=Changed momentumPhase=None
NSEvent: type=ScrollWheel loc=(1036.14,554.34) time=8104.1 flags=0 win=0x10dd080c0 winNum=890 ctxt=0x0 deltaX=-152.000000 deltaY=0.000000 count:2 phase=None momentumPhase=Began

由于没有这样的变量或方法,我如何从NSEvent中实际获取“计数”?

1 个答案:

答案 0 :(得分:0)

感谢@Willeke,这是Swift版本:

override func scrollWheel( with event: NSEvent )
{
    print( event )
    if let count = event.cgEvent?.getIntegerValueField( CGEventField.scrollWheelEventScrollCount )
    {
        print( count )
    }
} 
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12769.8 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=0.000000 deltaY=0.000000 count:0 phase=MayBegin momentumPhase=None
0
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12769.8 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=-5.000000 deltaY=0.000000 count:0 phase=Began momentumPhase=None
0
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12769.8 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=-18.000000 deltaY=1.000000 count:0 phase=Changed momentumPhase=None
0
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12769.9 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=-35.000000 deltaY=2.000000 count:1 phase=Changed momentumPhase=None
1
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12769.9 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=-84.000000 deltaY=4.000000 count:1 phase=Changed momentumPhase=None
1
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12769.9 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=-92.000000 deltaY=6.000000 count:1 phase=Changed momentumPhase=None
1
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12769.9 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=-103.000000 deltaY=9.000000 count:1 phase=Changed momentumPhase=None
1
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12770.1 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=0.000000 deltaY=0.000000 count:1 phase=None momentumPhase=Ended
1
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12770.1 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=-25.000000 deltaY=0.000000 count:2 phase=Began momentumPhase=None
2
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12770.1 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=-84.000000 deltaY=2.000000 count:2 phase=Changed momentumPhase=None
2
NSEvent: type=ScrollWheel loc=(824.82,567.461) time=12770.1 flags=0 win=0x10db2fa10 winNum=1327 ctxt=0x0 deltaX=-89.000000 deltaY=3.000000 count:2 phase=Changed momentumPhase=None