objective-c – 为什么CGWarpMouseCursorPosition导致延迟?如果不是,那是什么?

前端之家收集整理的这篇文章主要介绍了objective-c – 为什么CGWarpMouseCursorPosition导致延迟?如果不是,那是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我这里的代码将鼠标限制在屏幕上的一个区域,它工作得相对较好,只有一个大问题.当沿着区域的边缘运行时,鼠标没有干净/平滑地移动,而是以非常波动的方式跳跃,我相信这可能是由于CGWarpMouseCursorPosition导致每次“扭曲”的延迟.

任何人都可以告诉我的代码中是否有导致此延迟的事情,或者它是否实际上是鼠标扭曲函数.如果是鼠标扭曲功能,有什么方法可以让鼠标顺利重新定位?我在闪存中做了同样的事情并且它完美无瑕地工作,我知道循环不仅花费了太多时间来执行它减慢了速度因为它只运行了4到5次.

  1. CGEventRef
  2. mouse_filter(CGEventTapProxy proxy,CGEventType type,CGEventRef event,void *refcon) {
  3.  
  4.  
  5. CGPoint point = CGEventGetLocation(event);
  6.  
  7. float tX = point.x;
  8. float tY = point.y;
  9.  
  10. if( tX <= 700 && tX >= 500 && tY <= 800 && tY >= 200){
  11. // target is inside O.K. area,do nothing
  12. }else{
  13.  
  14. CGPoint target;
  15.  
  16. //point inside restricted region:
  17. float iX = 600; // inside x
  18. float iY = 500; // inside y
  19.  
  20. // delta to midpoint between iX,iY and tX,tY
  21. float dX;
  22. float dY;
  23.  
  24. float accuracy = .5; //accuracy to loop until reached
  25.  
  26. do {
  27. dX = (tX-iX)/2;
  28. dY = (tY-iY)/2;
  29.  
  30. if((tX-dX) <= 700 && (tX-dX) >= 500 && (tY-dY) <= 800 && (tY-dY) >= 200){
  31. iX += dX;
  32. iY += dY;
  33. } else {
  34. tX -= dX;
  35. tY -= dY;
  36. }
  37.  
  38. } while (abs(dX)>accuracy || abs(dY)>accuracy);
  39.  
  40. target = CGPointMake(roundf(tX),roundf(tY));
  41. CGWarpMouseCursorPosition(target);
  42.  
  43. }
  44.  
  45.  
  46.  
  47. return event;
  48. }
  49.  
  50. int
  51. main(int argc,char *argv[]) {
  52. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  53. CFRunLoopSourceRef runLoopSource;
  54. CGEventMask event_mask;
  55. event_mask = CGEventMaskBit(kCGEventMouseMoved) | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged) | CGEventMaskBit(kCGEventOtherMouseDragged);
  56.  
  57. CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap,kCGHeadInsertEventTap,event_mask,mouse_filter,NULL);
  58.  
  59. if (!eventTap) {
  60. NSLog(@"Couldn't create event tap!");
  61. exit(1);
  62. }
  63.  
  64. runLoopSource = CFMachPortCreateRunLoopSource(kcfAllocatorDefault,eventTap,0);
  65.  
  66. CFRunLoopAddSource(CFRunLoopGetCurrent(),runLoopSource,kcfRunLoopCommonModes);
  67.  
  68. CGEventTapEnable(eventTap,true);
  69.  
  70. CFRunLoopRun();
  71.  
  72. CFRelease(eventTap);
  73. CFRelease(runLoopSource);
  74. [pool release];
  75.  
  76. exit(0);
  77. }

解决方法

正如您所发现的,CGSetLocalEventsSuppressionInterval可以解决您的问题.

但是,从10.6开始,它已被弃用. Apple文档声明:

This function is not recommended for general use because of undocumented special cases and undesirable side effects. The recommended replacement for this function is CGEventSourceSetLocalEventsSuppressionInterval,which allows the suppression interval to be adjusted for a specific event source,affecting only events posted using that event source.

不幸的是,replacementCGEventSourceSetLocalEventsSuppressionInterval不适用于CGWarpMouseCursorPosition移动.

相反,在warp之后立即使用CGAssociateMouseAndMouseCursorPosition(true):

  1. CGPoint warpPoint = CGPointMake(42,42);
  2. CGWarpMouseCursorPosition(warpPoint);
  3. CGAssociateMouseAndMouseCursorPosition(true);

文档没有提到这种行为,但似乎取消了warp之后的抑制间隔.

猜你在找的C&C++相关文章