如何将字符串转换为一系列的击键? [英] How to convert a string into a series of keystrokes?

查看:155
本文介绍了如何将字符串转换为一系列的击键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 CGPostKeyboardEvent((CGCharCode)0,(CGKeyCode)55,true); 来模拟按键,但说我有一个字符串,转换为按键。我如何将NSString划分为单个字符,然后将它们转换为键盘?感谢!



编辑:OK我想我找到了一个解决方案,但我无法获得我的语法。 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;
//什么是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 ... \\\
,keyString);
exit(EXIT_FAILURE);因此我的问题是,如何获得
}


$ b <解决方案 $

div>

这个问题是一个噩梦,因为有功能将键代码转换为字符代码,但不是反过来。请记住,并非所有字符都可以输入,哪些字符可以输入取决于当前的键盘布局。



如果您的目标是让某些文本出现在另一个应用程序中,您可以通过使用 CGEventKeyboardSetUnicodeString 而不转换单个字符来执行此操作。但是,此功能的实施已经很多,因此请务必在您关心的所有操作系统版本以及Carbon和Cocoa应用中测试。


I'm using CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)55, true); to simulate key presses, but say I had a string that I wanted to convert to key presses. How would I go about dividing an NSString up into single characters, and then converting those into key presses? Thanks!

Edit: OK I think I found a solution, but I am unable to get my syntax right. Objective-C really drives me crazy.

Here's my code:

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);
}

So my question is, How do I get the keyCodeForKeyString(currentCharacter) inside the argument of CGPostKeyboardEvent?

解决方案

That problem is a nightmare, because there are functions to translate key codes to character codes, but not the other way around. Remember that not all characters can be typed, and which ones can be typed depends on the current keyboard layout.

If your goal is to make some text appear in another application, you may be able to do it without translating individual characters by using CGEventKeyboardSetUnicodeString. However, implementation of this feature has been spotty, so be sure to test on all OS versions you care about, and in both Carbon and Cocoa apps.

这篇关于如何将字符串转换为一系列的击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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