I need to take a QString (for example "A") and transform this string to 0x41. The input is a QString and the output must be a "WORD" (microsoft stuff).
a WORD is a unsigned short int in my case. I have tried :
Qstring str = "A"
qDebug() << str.toLatin1().toHex().prepend("0x");
the result is good "0x41" but it's not a unsigned short int :( i have tried cast, but it doesn't work.
the commun answer seem use "toUShort" but my code doesn't work with this change. I want emulate a keyboard with the dll winuser ( https://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx ) :
this code work :
INPUT key;
key.type = INPUT_KEYBOARD;
key.ki.wScan = 0; // hardware scan code for key
key.ki.time = 0;
key.ki.dwExtraInfo = 0;
key.ki.wVk = 0x42; // virtual-key code for the "a" key
key.ki.dwFlags = 0; // 0 for key press
SendInput(1, &key, sizeof(INPUT));
key.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &key, sizeof(INPUT));
that no :
QString str = "a";
bool ok;
INPUT key;
key.type = INPUT_KEYBOARD;
key.ki.wScan = 0; // hardware scan code for key
key.ki.time = 0;
key.ki.dwExtraInfo = 0;
key.ki.wVk = str.toUShort(&ok,16);
key.ki.dwFlags = 0; // 0 for key press
SendInput(1, &key, sizeof(INPUT));
key.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &key, sizeof(INPUT));
maybe it's just because short return 42 and not 0x42.
-- SOLUTION --
void SyncKeyboard::writeUID(QString uid)
{
qDebug() << uid;
for(int i = 0; i < uid.length(); i++)
pressKey(uid.at(i).unicode());
}
void SyncKeyboard::pressKey(ushort letter)
{
INPUT key;
key.type = INPUT_KEYBOARD;
key.ki.wScan = 0; // hardware scan code for key
key.ki.time = 0;
key.ki.dwExtraInfo = 0;
key.ki.wVk = letter;
key.ki.dwFlags = 0; // 0 for key press
SendInput(1, &key, sizeof(INPUT));
key.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &key, sizeof(INPUT));
}
如果只需要获取一个无符号的短整数,即A
的十六进制ASCII码,则可以尝试使用QByteArray :: toUShort 。
ushort QByteArray::toUShort ( bool * ok = 0, int base = 16 ) const
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.