MacBook 键盘双击问题

20200219151908.jpg

MacBook新出的蝴蝶键盘出现双击真的难受,关键我出现问题的是V,每次粘贴的时候会粘贴两次,Google找解决方案,没想到还真找到了。解决 MacBook 键盘双击问题

我这里整理下,做个搬运工。

// kill_double_typing.cpp
// http://osxbook.com
//
// You need superuser privileges to create the event tap, unless accessibility
// is enabled. To do so, select the "Enable access for assistive devices"
// checkbox in the Universal Access system preference pane.

// modified by SF-Zhou
// To: Kill Double Typing on MacBook
// Complile: g++ -O2 -Wall -o kill_double_typing kill_double_typing.cpp -framework ApplicationServices
// Run: ./kill_double_typing

#include 
#include 
#include 
#include 
using namespace std;

typedef chrono::time_point Time;
typedef long long ll;

unordered_map last_time;

Time time_now() {
  return chrono::high_resolution_clock::now();
}

// This callback will be invoked every time there is a keystroke.
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
  // Paranoid sanity check.
  if ((type != kCGEventKeyDown) && (type != kCGEventKeyUp)) {
    return event;
  }

  // The incoming keycode.
  CGKeyCode keycode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);

  // printf("%d\n", keycode);  // 打印键盘keycode,把注释去掉来找到你的双击按键对应的keycode,然后改下面的的代码,我这里是v和n
  if (keycode == 9 /* v */ || keycode == 45 /* n */) {
    if (type == kCGEventKeyUp) {
      last_time[keycode] = time_now();
    } else {
      if (last_time.count(keycode)) {
        ll microseconds = chrono::duration_cast(
          time_now() - last_time[keycode]
        ).count();

        //  60ms内双击忽略,可以根据你的需要修改
        if (microseconds < 60000) {
          return NULL;
        }
      }
    }
  }

  // Set the modified keycode field in the event.
  CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, (int64_t)keycode);

  // We must return the event for it to be useful.
  return event;
}

int main(void) {
  CFMachPortRef      eventTap;
  CGEventMask        eventMask;
  CFRunLoopSourceRef runLoopSource;

  // Create an event tap. We are interested in key presses.
  eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp));
  eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, myCGEventCallback, NULL);
  if (!eventTap) {
      fprintf(stderr, "failed to create event tap\n");
      exit(1);
  }

  // Create a run loop source.
  runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);

  // Add to the current run loop.
  CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);

  // Enable the event tap.
  CGEventTapEnable(eventTap, true);

  // Set it all running.
  CFRunLoopRun();

  // In a real program, one would have arranged for cleaning up.

  exit(0);
}

复制上面代码,保存到文件 kill_double_typing.cpp 中,然后在保存文件目录下用终端执行 g++ -O2 -Wall -o kill_double_typing kill_double_typing.cpp -framework ApplicationServices,完成后当前目录下会生成kill_double_typing文件, 双击就可以运行。把printf("%d\n", keycode);这行注释去掉,找到你的有问题的键,最终使用的时候要把把这行再注释回去。

接下来,实现开机启动。Mac设置开机启动
第一步,把编译生成的文件copy到你想存放的目录,例如:/Users/user_name/APP/kill_double_typing

执行vim ~/Library/LaunchAgents/kill_double_typing.plist,添加下面代码





    Label
    com.kill.double_typing
    ProgramArguments
    
        nohup
        /Users/user_name/APP/kill_double_typing
    
    KeepAlive
    
    RunAtLoad
    


不熟悉vim的同学的,可以切到~/Library/LaunchAgents/目录下,手动新建文件。

检查plist语法是否正确

plutil ~/Library/LaunchAgents/kill_double_typing.plist

载入配置, 使配置生效

launchctl load ~/Library/LaunchAgents/kill_double_typing.plist

卸载配置

launchctl unload ~/Library/LaunchAgents/kill_double_typing.plist

查看服务运行状态

launchctl list

完工。

参考文章:
解决 MacBook 键盘双击问题
Mac设置开机启动

你可能感兴趣的:(MacBook 键盘双击问题)