How to run a function when a key is pressed? - python-3.x

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.

Related

Computer keyboard into piano keyboard with AutoHotkey

I want to be able to use my computer keyboard as a piano keyboard, however the default version of AutoHotkey only supports one "voice" at a time. I tried running an instance for each note, but that doesn't fix it if I press the same note repeatedly.
I found this thread on how this might be solved with the BASS library, but I'm pretty green when it comes to coding and so I'm not certain how to incorporate the library into my simple code.
Here's another similar forum that might solve things, but it has a delay and the overlapping solution doesn't really solve my issue.
This is such a simple idea (play sound when a button is pressed), but somehow it's way out of my depth. Currently my code looks like this:
~1::
SoundPlay, C:\Users\Fires\Downloads\2489__jobro__piano-ff\39187__jobro__piano-ff-040.wav
for each note
Edit:
~a::
FileDelete, %A_ScriptDIR%\Sound1.AHK
FileAppend,
(
SoundPlay, C:\Users\Fires\Desktop\New folder (4)\043.wav, Wait
), %A_ScriptDir%\Sound1.AHK
Run, %A_ScriptDIR%\Sound1.AHK
Return
is what I am using now, but it's still iffy when two are pressed at the same time.
Its likely due to the "Wait". according to the documentation:
https://autohotkey.com/docs/commands/SoundPlay.htm
"If a file is playing and the current script plays a second file, the first file will be stopped so that the second one can play. On some systems, certain file types might stop playing even when an entirely separate script plays a new file."
It looks like this is an "issue" with AutoHotKey. And its not possible to open multiple "voices" or simultaneous sounds. it is completely possible to make a C# program that does the same thing, I've done it before. and i have bits of the code now still (it plays midi sounds, instead of wavs, but same concept play notes asynchronously).

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

Recognition of keyboard event in python

I'm a beginning programmer and i need a way to get python to recognize a keyboard event. So far i can only find these methods in vPython or tkinter, and i want to simply use the normal python shell. if anyone can help me with a module name/where i could download a module for free that would be very helpful. or simply the code if there is no need for a module.Thanks
There is no generic "keyboard event", they all depend on what your environment is.
In a terminal there are no keyboard events at all, you simply get sent text on stdin. In Windows you need to use the Win32, on Unix you need to use the X11 API and on Mac you have to use whatever OS X uses is it Cocoa?). wxPython and tkinter all work on all these platforms to provide an API that works on all of them. And the same goes for other GUI toolkits of the same sort like KDE and GTK. So you should use one of those. Which one is a matter of taste, but since tkinter is included in Python, that seems the obvious choice.
You can call the respective API's for the respective platforms directly, but it's generally not worth the trouble.

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.

How do I tell if a button is being held down in OpenGL/Glut? (linux)

This is a similar problem: Link
Which was solved by calling GetAsyncKeyState(). While all fine and dandy, I need a Linux alternative. I need to know if a button is being held down, not just being pressed (because of the keyboard buffer delay). Does anything like this exist in the OpenGL/Glut libraries, or will I have to look elsewhere?
I have never used Glut, but I know that many people will say SDL is better. I have used SDL and I like it a lot. It does everything Glut does and a lot more. In SDL, you can use SDL_PollEvent() to get key state without the keyboard buffer delay.
Edit: I know almost nothing about Glut, but it looks like you can use glutKeyboardFunc to detect normal keys, and glutSpecialFunc for keys that do not generate ASCII characters (such as shift). I'm not sure if there is a better way, as this doesn't seem very nice.
You can detect when a keypress event occurs, record that state, and then listen for a key release event.
As said, you will have to make your own state machine, which is easy. But you also need to use this callback method I think.
http://pyopengl.sourceforge.net/documentation/manual/glutKeyboardUpFunc.3GLUT.xml

Resources