McRogueFace/src/PyKeyboard.cpp
John McCardle c025cd7da3 feat: Add Sound/Music classes, keyboard state, version (#66, #160, #164)
Replace module-level audio functions with proper OOP API:
- mcrfpy.Sound: Wraps sf::SoundBuffer + sf::Sound for short effects
- mcrfpy.Music: Wraps sf::Music for streaming long tracks
- Both support: volume, loop, playing, duration, play/pause/stop
- Music adds position property for seeking

Add mcrfpy.keyboard singleton for real-time modifier state:
- shift, ctrl, alt, system properties (bool, read-only)
- Queries sf::Keyboard::isKeyPressed() directly

Add mcrfpy.__version__ = "1.0.0" for version identity

Remove old audio API entirely (no deprecation - unused in codebase):
- createSoundBuffer, loadMusic, playSound
- setMusicVolume, getMusicVolume, setSoundVolume, getSoundVolume

closes #66, closes #160, closes #164

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 16:24:27 -05:00

64 lines
2.6 KiB
C++

#include "PyKeyboard.h"
#include "McRFPy_Doc.h"
PyObject* PyKeyboard::repr(PyObject* obj)
{
// Show current state in repr for debugging
bool shift = sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RShift);
bool ctrl = sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RControl);
bool alt = sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RAlt);
bool system = sf::Keyboard::isKeyPressed(sf::Keyboard::LSystem) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RSystem);
std::ostringstream ss;
ss << "<Keyboard shift=" << (shift ? "True" : "False")
<< " ctrl=" << (ctrl ? "True" : "False")
<< " alt=" << (alt ? "True" : "False")
<< " system=" << (system ? "True" : "False") << ">";
std::string repr_str = ss.str();
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
}
PyObject* PyKeyboard::get_shift(PyObject* self, void* closure)
{
bool pressed = sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RShift);
return PyBool_FromLong(pressed);
}
PyObject* PyKeyboard::get_ctrl(PyObject* self, void* closure)
{
bool pressed = sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RControl);
return PyBool_FromLong(pressed);
}
PyObject* PyKeyboard::get_alt(PyObject* self, void* closure)
{
bool pressed = sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RAlt);
return PyBool_FromLong(pressed);
}
PyObject* PyKeyboard::get_system(PyObject* self, void* closure)
{
bool pressed = sf::Keyboard::isKeyPressed(sf::Keyboard::LSystem) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RSystem);
return PyBool_FromLong(pressed);
}
PyGetSetDef PyKeyboard::getsetters[] = {
{"shift", (getter)PyKeyboard::get_shift, NULL,
MCRF_PROPERTY(shift, "True if either Shift key is currently pressed (read-only)."), NULL},
{"ctrl", (getter)PyKeyboard::get_ctrl, NULL,
MCRF_PROPERTY(ctrl, "True if either Control key is currently pressed (read-only)."), NULL},
{"alt", (getter)PyKeyboard::get_alt, NULL,
MCRF_PROPERTY(alt, "True if either Alt key is currently pressed (read-only)."), NULL},
{"system", (getter)PyKeyboard::get_system, NULL,
MCRF_PROPERTY(system, "True if either System key (Win/Cmd) is currently pressed (read-only)."), NULL},
{NULL}
};