简体   繁体   English

使用SDL2捕获按键

[英]Using SDL2 for Capturing Key strokes

Have 2 questions related to using SDL2. 有2个与使用SDL2有关的问题。

  1. I want to capture all the keyboard inputs in my C++ app. 我想捕获我的C ++应用程序中的所有键盘输入。 The app is a COM dll that will run in the background and will capture all the inputs. 该应用程序是一个COM dll,将在后台运行并捕获所有输入。 So this app doesn't have a window, in this case is it possible to capture keyboard inputs? 因此,此应用没有窗口,在这种情况下可以捕获键盘输入吗? If yes, how? 如果是,怎么办?

I have seen examples for SDL2 where they 1st create a SDL window and then capture keyboard inputs. 我看到了SDL2的示例,在该示例中,他们首先创建了一个SDL窗口,然后捕获了键盘输入。 But i don't want that. 但是我不想要那样。 What i want is my app to be able to capture all keyboard inputs by its own. 我想要的是我的应用程序能够自己捕获所有键盘输入。

  1. If i create my own window using MFC, is it possible to attach that to SDL2 and have it capture all keyboard inputs when my window is in focus. 如果我使用MFC创建自己的窗口,则可以将其附加到SDL2上,并在窗口聚焦时捕获所有键盘输入。 Again i don't want to create a SDL Window. 同样,我不想创建一个SDL窗口。

you can use GetAsyncKeyState it's simple and fast, this code shows you how to use it. 您可以简单快速地使用GetAsyncKeyState,此代码向您展示如何使用它。

if(GetAsyncKeyState('Q')){//if Q key has been clicked
   //Do something
}

and here is list of all key codes http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx 这是所有关键代码的列表http://msdn.microsoft.com/zh-cn/library/windows/desktop/dd375731(v=vs.85).aspx

So this app doesn't have a window, in this case is it possible to capture keyboard inputs? 因此,此应用没有窗口,在这种情况下可以捕获键盘输入吗?

Not with SDL. 不适用于SDL。

If i create my own window using MFC, is it possible to attach that to SDL2 and have it capture all keyboard inputs when my window is in focus. 如果我使用MFC创建自己的窗口,则可以将其附加到SDL2上,并在窗口聚焦时捕获所有键盘输入。

It sounds like you might have some luck with SDL_CreateWindowFrom() . 听起来SDL_CreateWindowFrom()可能会有些运气。

You could try to have an window with the SDL_WINDOW_HIDDEN flag. 您可以尝试使用带有SDL_WINDOW_HIDDEN标志的窗口。 That way i would assume you could get he input with the sdl functions without having an window pop. 这样,我假设您可以在没有弹出窗口的情况下让他输入sdl函数。

Not sure if this is what you want, but i hope it helps. 不确定这是否是您想要的,但我希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM