如何模拟截图功能(Mac)Cocoa? [英] How do I simulate the screenshot function (Mac) Cocoa?

查看:34
本文介绍了如何模拟截图功能(Mac)Cocoa?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前不起作用的代码:

Here is my current code that DOES NOT work:

CGEventSourceRef src =
CGEventSourceCreate(kCGEventSourceStateHIDSystemState);

CGEventRef cmdd = CGEventCreateKeyboardEvent(src, 0x37, true);
CGEventRef cmdu = CGEventCreateKeyboardEvent(src, 0x37, false);
CGEventRef optd = CGEventCreateKeyboardEvent(src, 0x3A, true);
CGEventRef optu = CGEventCreateKeyboardEvent(src, 0x3A, false);
CGEventRef dd = CGEventCreateKeyboardEvent(src, 0x02, true);
CGEventRef du = CGEventCreateKeyboardEvent(src, 0x02, false);

CGEventSetFlags(dd, kCGEventFlagMaskCommand ^ kCGEventFlagMaskAlternate);
CGEventSetFlags(du, kCGEventFlagMaskCommand ^ kCGEventFlagMaskAlternate);


CGEventTapLocation loc = kCGHIDEventTap; // kCGSessionEventTap also works
CGEventPost(loc, cmdd); //Cmd down
CGEventPost(loc, optd); //Option down
CGEventPost(loc, dd);   //D down
CGEventPost(loc, cmdu); //Cmd up
CGEventPost(loc, optu); //Option up
CGEventPost(loc, du);   //D up

CFRelease(cmdd);
CFRelease(cmdu);
CFRelease(optd);
CFRelease(optu);
CFRelease(dd);
CFRelease(du);
CFRelease(src);

我需要更改什么才能使其正常工作?有谁知道为什么它无法工作?

What do I need to change to make it work? Does anyone know why it fails to work?

推荐答案

我设法解决了我的问题

我是这样做的:

  1. 我制作了一个执行命令screencapture -s/temp/tempscr/png"的shell脚本文件
  2. 然后我将 .sh 文件复制到我的应用程序中的Supporting Files"文件夹中
  3. 然后我执行了以下代码:

  1. I made a shell script file that executed the command "screencapture -s /temp/tempscr/png"
  2. I then copied the .sh file into my "Supporting Files" folder within my app
  3. I then executed the following code:

NSString *path = @"/Applications/CloudSyncMac.app/Contents/Resources/screencapture.sh";
NSArray *args = [NSArray arrayWithObjects:nil, nil];
[[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];

  • 有效!!!

  • It works!!!

    这篇关于如何模拟截图功能(Mac)Cocoa?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆