Advertisement
KidaCoding

Untitled

Jan 14th, 2025 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <thread>
  4. #include <atomic>
  5. #include <chrono>
  6. #include <mutex>
  7.  
  8. std::atomic<bool> isRunning(false);
  9. std::thread simulationThread;
  10. std::mutex simulationMutex;
  11.  
  12. HWND currentWindowHandle = nullptr;
  13.  
  14. void simulateSingleKeyPress(WORD key) {
  15. INPUT input = { 0 };
  16. input.type = INPUT_KEYBOARD;
  17. input.ki.wVk = key;
  18.  
  19. SendInput(1, &input, sizeof(INPUT));
  20.  
  21. input.ki.dwFlags = KEYEVENTF_KEYUP;
  22. SendInput(1, &input, sizeof(INPUT));
  23. }
  24.  
  25. void keyPressTimer(WORD key, int delayMilliseconds) {
  26. while (true) {
  27. {
  28. std::lock_guard<std::mutex> lock(simulationMutex);
  29. if (!isRunning) break;
  30. }
  31.  
  32. if (GetForegroundWindow() == currentWindowHandle) {
  33. simulateSingleKeyPress(key);
  34. }
  35.  
  36. std::this_thread::sleep_for(std::chrono::milliseconds(delayMilliseconds));
  37. }
  38. }
  39.  
  40. void startKeyPressSimulation(WORD key, int delayMilliseconds) {
  41. std::lock_guard<std::mutex> lock(simulationMutex);
  42.  
  43. if (isRunning) {
  44. std::cout << "Simulation is already running.\n";
  45. return;
  46. }
  47.  
  48. currentWindowHandle = GetConsoleWindow();
  49. if (!currentWindowHandle) {
  50. std::cout << "Failed to get the console window handle.\n";
  51. return;
  52. }
  53.  
  54. isRunning = true;
  55. simulationThread = std::thread(keyPressTimer, key, delayMilliseconds);
  56.  
  57. if (simulationThread.native_handle()) {
  58. SetThreadPriority(simulationThread.native_handle(), THREAD_PRIORITY_BELOW_NORMAL);
  59. }
  60.  
  61. std::cout << "Simulation started.\n";
  62. }
  63.  
  64. void stopKeyPressSimulation() {
  65. {
  66. std::lock_guard<std::mutex> lock(simulationMutex);
  67. if (!isRunning) {
  68. std::cout << "Simulation is not running.\n";
  69. return;
  70. }
  71.  
  72. isRunning = false;
  73. }
  74.  
  75. if (simulationThread.joinable()) {
  76. simulationThread.join();
  77. }
  78.  
  79. std::cout << "Simulation stopped.\n";
  80. }
  81.  
  82. int main() {
  83. const WORD key = VK_F11;
  84. const int delay = 1000;
  85.  
  86. std::cout << "Key Press Simulator\n";
  87. std::cout << "1: Start Simulation\n";
  88. std::cout << "2: Stop Simulation\n";
  89. std::cout << "3: Exit\n";
  90.  
  91. int choice;
  92. do {
  93. std::cout << "\nEnter your choice: ";
  94. std::cin >> choice;
  95.  
  96. switch (choice) {
  97. case 1:
  98. startKeyPressSimulation(key, delay);
  99. break;
  100. case 2:
  101. stopKeyPressSimulation();
  102. break;
  103. case 3:
  104. stopKeyPressSimulation();
  105. std::cout << "Exiting program.\n";
  106. break;
  107. default:
  108. std::cout << "Invalid choice. Try again.\n";
  109. }
  110. } while (choice != 3);
  111.  
  112. return 0;
  113. }
  114.  
Tags: MS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement