summaryrefslogtreecommitdiff
path: root/key.cpp
diff options
context:
space:
mode:
authorjStmaX! <87650746+jstmaxlol@users.noreply.github.com>2025-08-20 16:10:28 +0200
committerGitHub <noreply@github.com>2025-08-20 16:10:28 +0200
commit0b670cd6b47b17f19dc9df2f0e43221d4c7e6738 (patch)
tree7e18caeda9e22237cc6680fab4691e8e8a452976 /key.cpp
parented420b2f6188e48479329d8d5be4d06e61ceadfd (diff)
random utils in /usr/bin because dementia :e
Diffstat (limited to 'key.cpp')
-rw-r--r--key.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/key.cpp b/key.cpp
new file mode 100644
index 0000000..cc3441b
--- /dev/null
+++ b/key.cpp
@@ -0,0 +1,46 @@
+#include <iostream>
+#include <termios.h>
+#include <unistd.h>
+#include <escape.ansi>
+
+char getch() {
+ struct termios oldt, newt;
+ char ch;
+ tcgetattr(STDIN_FILENO, &oldt); // Get the current terminal settings
+ newt = oldt; // Copy the settings
+ newt.c_lflag &= ~(ICANON | ECHO); // Disable canonical mode and echo
+ tcsetattr(STDIN_FILENO, TCSANOW, &newt); // Set the new settings
+ ch = getchar(); // Read a character
+ tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // Restore the old settings
+ return ch;
+}
+
+int main() {
+ std::cout << "Press " << red << "any key" << def << " to see its ASCII keycode. | Press " << red << "ESC" << def << " to exit." << std::endl;
+
+ while (true) {
+ char ch = getch(); // Get a character from the user
+
+ // Check for ESC key to exit
+ if (ch == 27) { // ASCII code for ESC
+ std::cout << "ESC = 27\n" << red << "Quitting ..\n" << def;
+ break;
+ }
+
+ if (ch == 32) {
+ std::cout << "SPACEBAR = " << static_cast<int>(ch) << std::endl;
+ } else if (ch == 9) {
+ std::cout << "TAB = " << static_cast<int>(ch) << std::endl;
+ } else if (ch == 127) {
+ std::cout << "BACKSPACE = " << static_cast<int>(ch) << std::endl;
+ } else if (ch == 10) {
+ std::cout << "ENTER = " << static_cast<int>(ch) << std::endl;
+ } else {
+ // Output pressed key
+ std::cout << ch << " = " << static_cast<int>(ch) << std::endl;
+ }
+ }
+
+ return 0;
+}
+