为什么我的功能在被调用时不起作用?

时间:2015-01-19 07:08:26

标签: c++ windows

所以我正在创建一个模拟按键“a”的程序,然后使用我的回车键功能按Enter键,但是该功能不起作用。当我运行程序时,它会点击一个键,但似乎函数hitDaEnterKey根本不起作用。我做错了什么?

#define WINVER 0x0500
#include <windows.h>



int hitDaEnterKey()
{
    INPUT ip;

    // Press and release Enter Key
    ip.ki.wVk = 0x0D;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));
    // release
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT)); 

    return 0;
}

int main()
{

    // This structure will be used to create the keyboard
    // input event.
    INPUT ip;


    Sleep(2500);

    // Set up a generic keyboard event.
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = 0; // hardware scan code for key
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0; 

    // Press the "A" key

    ip.ki.wVk = 0x41; // virtual-key code for the "a" key
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));

    // Release the "A" key

    ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
    SendInput(1, &ip, sizeof(INPUT)); 

    hitDaEnterKey();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

int hitDaEnterKey()
{
    INPUT ip;

    // Set up a generic keyboard event.
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = 0; // hardware scan code for key
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;

    // Press and release Enter Key
    ip.ki.wVk = 0x0D;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));
    // release
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT)); 

    return 0;
}

    int hitDaEnterKey(INPUT *ip)
    {        
        // Press and release Enter Key
        ip->ki.wVk = 0x0D;
        ip->ki.dwFlags = 0;
        SendInput(1, ip, sizeof(INPUT));
        // release
        ip->ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(1, ip, sizeof(INPUT)); 

        return 0;
    }

int main()
{

    // This structure will be used to create the keyboard
    // input event.
    INPUT ip;


    Sleep(2500);

    // Set up a generic keyboard event.
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = 0; // hardware scan code for key
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0; 

    // Press the "A" key

    ip.ki.wVk = 0x41; // virtual-key code for the "a" key
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));

    // Release the "A" key

    ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
    SendInput(1, &ip, sizeof(INPUT)); 

    hitDaEnterKey(&ip);

    return 0;
}