使用`sendmessage()`发送表情符号。

huangapple go评论97阅读模式
英文:

C send emojis with sendmessage()

问题

我想使用sendmessage()函数向浏览器(如Chrome或Microsoft Edge)发送表情符号。

当我定义一个带有表情符号的字符串,并使用fputs()将其写入一个.txt文件时,输出的格式如下所示:❤❤ ++ 😘😘

字符串:snprintf(buffer, sizeof(buffer), "\U00002764\U00002764 ++ \U0001F618\U0001F618");
缓冲数组使用char数据类型。

使用sendmessage()函数时,我没有得到预期的输出。尝试发送maximizewindow命令有效。以下是我使用的代码片段:

  1. for (int i = 0; i < strlen(buffer); i++)
  2. {
  3. SendMessage(hwnd, WM_UNICHAR, (WPARAM)0, (LPARAM)buffer[i]);
  4. }

我还尝试过PostMessage()PostMessageW()

英文:

I want send emojis to a browser like chrome or Microsoft edge with the sendmessage() function.
When i defined a string with the emojis and write it with fputs() to a .txt file the output is the expected in format of ❤❤ ++ 😘😘.

The string: snprintf(buffer, sizeof(buffer), "\U00002764\U00002764 ++ \U0001F618\U0001F618");
The buffer array use the char datatype.

With the sendmessage() function i do not get the expected output. Trying to send a maximizewindow command works. Following is my codesnipped i use:

  1. for (int i = 0; i < strlen(buffer); i++)
  2. {
  3. SendMessage(hwnd, WM_UNICHAR, (WPARAM)0, (LPARAM)buffer[i]);
  4. }

I also tryed PostMessage() and PostMessageW().

答案1

得分: 0

我找不到任何方法在几个小时后使用SendMessage()WM_UNICHAR。但是对我来说,我找到了一个很好的替代方案,可以使用SendInput()。这些函数支持UTF32字符,并在我测试的每个窗口中都能正常工作。接下来,我将发布我的代码以帮助其他人。

  1. short si_ret;
  2. INPUT pInputs;
  3. pInputs.type = INPUT_KEYBOARD;
  4. pInputs.ki.wVk = 0; // 在dwFlags为零时使用普通字符,否则在Unicode上将wVk设置为零
  5. pInputs.ki.wScan = (WORD)0; // 使用L'❤'来发送UTF-32字符,或匹配的值(参见WEB或wprintf)
  6. pInputs.ki.dwFlags = KEYEVENTF_UNICODE;
  7. wchar_t text[] = L"❤❤😘😘";
  8. for (unsigned int x = 0; x < wcslen(text); x++)
  9. {
  10. pInputs.ki.wScan = (WORD)text[x];
  11. if (!(si_ret = SendInput(1, &pInputs, sizeof(pInputs)))) { printf("SendInput do not work [%i]\r\n", GetLastError()); return 0; }
  12. else { printf("SendInput ok [%i]\r\n", si_ret); }
  13. }

希望这有助于您。

英文:

I cant find any way to use SendMessage() with WM_UNICHAR after hours. But for me i found a good alternative solution with SendInput(). These function supports UTF32 characters and works in every window i tested. Following i will post my code to help others.

  1. short si_ret;
  2. INPUT pInputs;
  3. pInputs.type = INPUT_KEYBOARD;
  4. pInputs.ki.wVk = 0; //Use here normal characters when dwFlags zero, otherwise on unicode set wVk to zero
  5. pInputs.ki.wScan = (WORD)0; //Use L&#39;❤&#39; to send UTF-32 characters or the matching value (see WEB or wprintf)
  6. pInputs.ki.dwFlags = KEYEVENTF_UNICODE;
  7. wchar_t text[] = L&quot;❤❤&#128536;&#128536;&quot;;
  8. for(unsigned int x = 0; x &lt; wcslen(text); x++)
  9. {
  10. pInputs.ki.wScan = (WORD)text[x];
  11. if(!(si_ret = SendInput(1, &amp;pInputs, sizeof(pInputs)))) { printf(&quot;SendInput do not work [%i]\r\n&quot;, GetLastError()); return 0; }
  12. else { printf(&quot;SendInput ok [%i]\r\n&quot;, si_ret); }
  13. }

huangapple
  • 本文由 发表于 2023年6月29日 04:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定