Skip to content

Commit

Permalink
New simulation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kitswas committed Jul 14, 2023
1 parent e74c3b4 commit 67a8c6f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ set(PROJECT_SOURCES
src/simulation/mouseSim.hpp
src/simulation/keyboardSim.cpp
src/simulation/keyboardSim.hpp
src/simulation/simulate.cpp
src/simulation/simulate.hpp
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
Expand Down
2 changes: 2 additions & 0 deletions src/simulation/keyboardSim.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* @file keyboardSim.hpp
* @brief Simulates keyboard input in Windows.
*
* @note Do not include this file directly. Use @link simulate.hpp @endlink instead.
*/
#pragma once

Expand Down
2 changes: 2 additions & 0 deletions src/simulation/mouseSim.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* @file mouseSim.hpp
* @brief Simulates mouse input in Windows.
*
* @note Do not include this file directly. Use @link simulate.hpp @endlink instead.
*/
#pragma once

Expand Down
31 changes: 31 additions & 0 deletions src/simulation/simulate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "simulate.hpp"

bool blockExternalInput()
{
return BlockInput(TRUE);
}

bool unblockExternalInput()
{
return BlockInput(FALSE);
}

void clearExistingInput()
{
// Get the keyboard state
BYTE keyboardState[256];
GetKeyboardState(keyboardState);

// Cancel any existing input
// Iterates through all keys, checking if they are down, and releasing them if they are
for (int i = 0; i < 256; i++)
{
if (keyboardState[i] & 0x80)
{
// Release the key
keyUp(i);
}
// ostream_here << "0X" << std::hex << i << std::dec << "(" << i << ")"
// << "is" << (keyboardState[i] & 0x80 ? "down" : "up");
}
}
24 changes: 24 additions & 0 deletions src/simulation/simulate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "keyboardSim.hpp"
#include "mouseSim.hpp"

/** This function is used to block input from external devices.
* It stops keyboard and mouse input events from reaching applications.
* It is used to prevent the user from accidentally interfering with the program.
*
* https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-blockinput
*
* @note **Emergency Exit:** <kbd>CTRL</kbd> + <kbd>ALT</kbd> + <kbd>DEL</kbd>
* @see @link unblockExternalInput()
*/
bool blockExternalInput();

/** This function is used to unblock input from external devices.
* It allows keyboard and mouse input events to reach applications again.
*
* @see @link blockExternalInput()
*/
bool unblockExternalInput();

void clearExistingInput();

0 comments on commit 67a8c6f

Please sign in to comment.