How to simulate a key press in Python on a linux machine - linux

How do you simulate a key press in python on a linux machine?
This is for use with an emulator and making a bot which can play a game.
So primarily the 'wasd' keys, space and so on, this thread here is more or less what I want, however I believe that this solution is windows specific using
ctypes.windll
I believe that the main problem and why this is hard to do is to do with 'ScanCodes' and 'VKs', games tend to ignore as this is not how the user interacts with the game
So is there any linux workaround like the above for linux?
Any help is appreciated, thank you.

I had the same problem when using pyautogui. I just seemed to not have the right focus. Selecting the window in a few different ways did not help. Using xdo however, I managed to get the desired result.
Example:
from xdo import Xdo
xdo = Xdo()
win_id = xdo.get_active_window()
print(xdo.get_window_name(win_id))
xdo.send_keysequence_window(win_id, "Return")
More information:
https://github.com/rshk/python-libxdo

Related

Mirror windows on X (Linux)

I am required to write a very efficient application that will mirror the contents of an arbitrary external application multiple times (a lot of times) onto an area of my window, for Linux. On Windows, the way I used to do it was with the help of DwmRegisterThumbnail which would tell the compositor (Desktop Window Manager) that I want it to draw the thumbnail of that foreign window, which it anyway generates, onto a rectangle in my own window, when it composes the desktop image to be displayed to the user on the monitor. This is, I think, one of the lowest overhead ways to achieve my goal, on Windows. The goal is to have very minimal impact on the CPU, as the app will run on a pretty constrained machine. I never tested it against GDI or DirectX methods of copying the screen data, but I do not believe it is faster. Or maybe I am wrong, do correct me if so, please. Is there any other method faster on Windows? The limitations of this method include not being able to touch the actual image data, so no drawing on top of it for example, which is fine for my goal.
Now, my question is, what would be the best approach to achieve this on Linux? I have full liberty of choosing an appropiate X server, display manager if needed and also can write whatever software just to make it as low overhead as it is on Windows. Is there a similar API to the one on Windows for some Linux compositor, like Mutter or KWin, that works well? Or should I hook into X and copy image data from it? Would that eat a lot of CPU?
What's your experience and opinion? How should I take on this?
Thank you very much.

How to run a function when a key is pressed?

I've been searching on how to do it without pygame, but I haven't found a way.
Basically all I want to do is to press a key and play a sound, but I do not know how to register keystrokes in python.
edit: Forgot to mention, for Windows.
It (getting keypresses and playing sounds) is probably operating system specific, and you might want to use some widget toolkit (interfaced to Python). So consider using one of PyGTK, PyQt, PySDL2 or some other suitable toolkit. I guess that for a game, PySDL2 should be the most suitable.
You might focus on the WinAPI only and follow this.

How should I program a window manager in Qt?

I'm thinking about making a basic window manager for Linux based off of X11 and using the Qt toolkit. QML looks like a good language, so I'll probably use that. The problem is that I've never coded a WM before. I know that the ICCCM and EWMH will be required reading but beyond that my knowledge is foggy. Are there any other resources I should look into before jumping in?
The window manager itself will probably not be written in Qt, as Qt is just an interface for drawing graphics into the windows themselves
Start by reading up on some XLib/XCB documentation and getting some basic things showing up like windows with pix-maps(images) in them
Write some test QML/Qt applications to gain knowledge in that
Test, find bugs, cry, debug, rinse and repeat
P.S need any help? This sounds like a fun project :D

How can I send input to multiple terminal windows (not tabs)?

I use gnome-terminal (Ubuntu 10.10). I like it, though I'd be willing to switch to another for this feature.
Can anyone tell me how I can broadcast keystrokes to multiple windows? The closest thing I've found is the "Terminator" program, which allows for broadcasting to multiple tabs, but not to multiple windows. Apparently a similar feature was removed from v3 of Konsole when it went to v4 (no idea why). There are also similar capabilities in screen, but not between windows, as far as I can tell.
I've spent a number of hours looking, but no joy.
I'd also be willing for a general solution (input to multiple windows of any kind) that I could adapt for use with terminal windows.
Thanks.
Try with a software called keyboardcast: apt-get install keyboardcast
For the source: http://web.archive.org/web/20100130104001/http://desrt.mcmaster.ca/code/keyboardcast/

Coding a GTK+ application without window manager?

I want to code sth. that basically works like TiVo. Switch it on, you only see the menu or an output, so no underlying OS or anything else is directly visible to the user.
So I want to use Linux as base. Can you suggest a good base distribution?
Can I code a frontend without having a window-manager up and running?
If yes, is that possible with java-gnome or what language/gui-framework combination would you suggest?
If no, what's the minimal window manager that can handle fancy menus, etc?
What does it take to create video-overlays over a HD-stream? Are there some libraries I should take a look at?
Thanks
Yes. If you only have one window you don't need a window manager. Using X you can start some application and set it's position and size from the commandline (making it fullscreen). You might want to take a look at xinit if this is what you want. This is likely the easiest why to get something working. But another option is skip X and use DirectFB. If you want to display several windows, on the other hand, you need some sort of window manager to manage them.
As long as you run X there is no problem using java-gnome as framework if that's what you are confortable with. I guess you didn't mean to run the stock gnome applications, but code everything visible to the user yourself.
This very much depends on what you mean with fancy menus. If you mean transparancy and such you need a composite manager (if you don't just render everything yourself inside your application window). I'm not sure about this but I think you can run a composite manager independent from a window manager if you find that suitable. Again, this is if you run X. Using DirectFB transparency and such are done in a more simple way.
If you intend to write your own media player you should take a look at GStreamer. It can stream, decode and display video and also add video-overlays (among other things) and is extremly easy to use.
Minimalistic tiling window manages like Awesome, Ratpoison or XMonad may be useful as a base, otherwise you'll have to manage focus and window sizing yourself. It is normally fairly easy to make these invisible to the user.
Absolutely.
I wouldn't count on Gnome itself working without a window manager. Other than that... language doesn't matter.
Window managers only do window management. Menus and the like are the job of the widget toolkit. Anyways, Metacity.
... This one I have no clue about.

Resources