2012-07-11 43 views
6

我有以下代碼:SendInput()鍵盤 - 僅小寫

INPUT Input = { 0 }; 
Input.type  = INPUT_KEYBOARD; 
Input.mi.dwFlags = KEYEVENTF_EXTENDEDKEY; 
Input.ki.wVk = 'A'; // tried 0x41, (UCHAR)VkKeyScan('A') 
SendInput(1, &Input, sizeof(INPUT)); 

,但它產生我只是 '一'。如何強制它生成大寫字母?

謝謝。

+5

你有模擬按住Shift鍵,就像輸入 – Shenaniganz 2012-07-11 17:28:54

+0

一樣,正如Shenaniganz暗示的那樣,您正在模擬一個按鍵。你的鍵盤上有一個大寫的「A」鍵和一個小寫的「a」鍵嗎? – TheZ 2012-07-11 17:29:40

+1

'INPUT'類型包含一個nameelss聯合,所以你真的不應該分配給同一個變量的'mi'和'ki'成員。 – rodrigo 2012-07-11 19:44:11

回答

8

編輯:根據在評論羅德里戈回答一些修改。

INPUT Input = { 0 }; 
// shift key down 
Input.type  = INPUT_KEYBOARD; 
Input.ki.wVk = VK_LSHIFT; 
SendInput(1, &Input, sizeof(INPUT)); 

// 'a' key down 
Input.type  = INPUT_KEYBOARD; 
Input.ki.wVk = 'A'; 
SendInput(1, &Input, sizeof(INPUT)); 

// 'a' key release 
Input.type  = INPUT_KEYBOARD; 
Input.ki.dwFlags = KEYEVENTF_KEYUP; 
Input.ki.wVk = 'A'; 
SendInput(1, &Input, sizeof(INPUT)); 

// shift key release 
Input.type  = INPUT_KEYBOARD; 
Input.ki.dwFlags = KEYEVENTF_KEYUP; 
Input.ki.wVk = VK_LSHIFT; 
SendInput(1, &Input, sizeof(INPUT)); 

編輯:這裏是開啓瓶蓋的另一例代碼發送字母后/推卸:

INPUT Event = { 0 }; 

const SHORT key = VkKeyScan('a'); 
const UINT mappedKey = MapVirtualKey(LOBYTE(key), 0); 

// Press shift key 
Event.type = INPUT_KEYBOARD; 
Event.ki.dwFlags = KEYEVENTF_SCANCODE; 
Event.ki.wScan = MapVirtualKey(VK_LSHIFT, 0); 
SendInput(1, &Event, sizeof(Event)); 

// upper case 'A' 
Event.type = INPUT_KEYBOARD; 
Event.ki.dwFlags = KEYEVENTF_SCANCODE; 
Event.ki.wScan = mappedKey; 
SendInput(1, &Event, sizeof(Event)); 

// release upper case 'A' 
Event.type = INPUT_KEYBOARD; 
Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; 
Event.ki.wScan = mappedKey; 
SendInput(1, &Event, sizeof(Event)); 

// Release shift key 
Event.type = INPUT_KEYBOARD; 
Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; 
Event.ki.wScan = MapVirtualKey(VK_LSHIFT, 0); 
SendInput(1, &Event, sizeof(Event)); 

const SHORT key1 = VkKeyScan('A'); 
const UINT mappedKey1 = MapVirtualKey(LOBYTE(key1), 0); 

// lower case 'a' 
Event.type = INPUT_KEYBOARD; 
Event.ki.dwFlags = KEYEVENTF_SCANCODE; 
Event.ki.wScan = mappedKey1; 
SendInput(1, &Event, sizeof(Event)); 

// release lower case 'a' 
Event.type = INPUT_KEYBOARD; 
Event.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; 
Event.ki.wScan = mappedKey1; 
SendInput(1, &Event, sizeof(Event)); 

解釋這裏(我希望解釋是正確的):SendInput() for keyboard - only uppercase

+1

確實有人可能。好的電話在這裏留下問題(和答案)。 – 0xC0000022L 2012-07-11 17:30:55