gpt4 book ai didi

objective-c - 如何将字符串转换为一系列击键?

转载 作者:行者123 更新时间:2023-12-03 16:53:18 26 4
gpt4 key购买 nike

我正在使用CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)55, true);模拟按键,但假设我有一个字符串想要转换为按键。我如何将 NSString 分成单个字符,然后将它们转换为按键?谢谢!

编辑: 好的,我想我找到了解决方案,但我无法使语法正确。 Objective-C 真的让我发疯。

这是我的代码:

int stringLength = [theData length];
for (int i = 1; i <= stringLength; i++) {
char *currentCharacter = [theData characterAtIndex:i];
CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)keyCodeForKeyString(currentCharacter), true);
}

-(int)keyCodeForKeyString:(char *)keyString
{
if (strcmp(keyString, "a") == 0) return 0;
if (strcmp(keyString, "s") == 0) return 1;
if (strcmp(keyString, "d") == 0) return 2;
if (strcmp(keyString, "f") == 0) return 3;
if (strcmp(keyString, "h") == 0) return 4;
if (strcmp(keyString, "g") == 0) return 5;
if (strcmp(keyString, "z") == 0) return 6;
if (strcmp(keyString, "x") == 0) return 7;
if (strcmp(keyString, "c") == 0) return 8;
if (strcmp(keyString, "v") == 0) return 9;
// what is 10?
if (strcmp(keyString, "b") == 0) return 11;
if (strcmp(keyString, "q") == 0) return 12;
if (strcmp(keyString, "w") == 0) return 13;
if (strcmp(keyString, "e") == 0) return 14;
if (strcmp(keyString, "r") == 0) return 15;
if (strcmp(keyString, "y") == 0) return 16;
if (strcmp(keyString, "t") == 0) return 17;
if (strcmp(keyString, "1") == 0) return 18;
if (strcmp(keyString, "2") == 0) return 19;
if (strcmp(keyString, "3") == 0) return 20;
if (strcmp(keyString, "4") == 0) return 21;
if (strcmp(keyString, "6") == 0) return 22;
if (strcmp(keyString, "5") == 0) return 23;
if (strcmp(keyString, "=") == 0) return 24;
if (strcmp(keyString, "9") == 0) return 25;
if (strcmp(keyString, "7") == 0) return 26;
if (strcmp(keyString, "-") == 0) return 27;
if (strcmp(keyString, "8") == 0) return 28;
if (strcmp(keyString, "0") == 0) return 29;
if (strcmp(keyString, "]") == 0) return 30;
if (strcmp(keyString, "o") == 0) return 31;
if (strcmp(keyString, "u") == 0) return 32;
if (strcmp(keyString, "[") == 0) return 33;
if (strcmp(keyString, "i") == 0) return 34;
if (strcmp(keyString, "p") == 0) return 35;
if (strcmp(keyString, "RETURN") == 0) return 36;
if (strcmp(keyString, "l") == 0) return 37;
if (strcmp(keyString, "j") == 0) return 38;
if (strcmp(keyString, "'") == 0) return 39;
if (strcmp(keyString, "k") == 0) return 40;
if (strcmp(keyString, ";") == 0) return 41;
if (strcmp(keyString, "\\") == 0) return 42;
if (strcmp(keyString, ",") == 0) return 43;
if (strcmp(keyString, "/") == 0) return 44;
if (strcmp(keyString, "n") == 0) return 45;
if (strcmp(keyString, "m") == 0) return 46;
if (strcmp(keyString, ".") == 0) return 47;

fprintf(stderr, "keyString %s Not Found. Aborting...\n", keyString);
exit(EXIT_FAILURE);
}

所以我的问题是,我如何获得 keyCodeForKeyString(currentCharacter)CGPostKeyboardEvent 的参数内?

最佳答案

这个问题是一场噩梦,因为有一些函数可以将按键代码转换为字符代码,但反之则不然。请记住,并非所有字符都可以键入,可以键入哪些字符取决于当前的键盘布局。

如果您的目标是使某些文本出现在另一个应用程序中,则无需使用 CGEventKeyboardSetUnicodeString 翻译单个字符即可实现此目的。然而,此功能的实现一直参差不齐,因此请务必在您关心的所有操作系统版本以及 Carbon 和 Cocoa 应用程序中进行测试。

关于objective-c - 如何将字符串转换为一系列击键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3169594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com