INPUT, INPUT_KEYBOARD, ip没有在这个作用域中声明

INPUT, INPUT_KEYBOARD, ip were not declared in this scope

本文关键字:INPUT 作用域 声明 KEYBOARD ip      更新时间:2023-10-16
#include <windows.h>
int main()
{
    // This structure will be used to create the keyboard
    // input event.
    INPUT ip;
    // Pause for 10 seconds.
    Sleep(1000*10);
    // 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 "F5" key
    ip.ki.wVk = 0x74; // virtual-key code for the "F5" key
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));
    // Release the "F5" key
    ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
    SendInput(1, &ip, sizeof(INPUT));
    // Exit normally
    return 0;
}   

上面的代码是模拟按下F5按钮,但是在声明方面有一些错误。INPUT, INPUT_KEYBOARD, ip未在此范围内声明。如何解决?

如果不知道正在使用的编译器和平台工具集,就很难确定究竟是哪里出了问题。阅读精细手册说:

头文件 Winuser.h(包括Windows.h)

你在做什么

它还说:

使用

输入的问题

当我尝试使用INPUT结构时,我的编译器告诉我这个结构是未声明的,但是我已经包含了<windows.h>。经过一番搜索,我找到了一个解决方案,<winable.h>也应该包括在内。我不知道为什么,但它有效。

MarrySunny
12/6/2011

尽管你最好使用最新版本的Microsoft Visual Studio和Windows SDK。

您正在尝试使用"SendInput()",这在某些版本的Windows中不可用。

一种可能是:

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

这可能会迫使Windows使用特定的版本…并且可能(取决于您的特定平台)完全像您想要的那样工作:)

文档如下:

  • http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx

另一种可能是替换旧的APi,如keybd_event:

  • http://msdn.microsoft.com/en-us/library/ee504289.aspx

当然,知道你的具体:

  • 平台:Windows 7?

  • 编译器:MSVS 2013?

  • 库:微软提供的Win32库?"别的东西"?