C++ Toggle to hold left click down? - c++

How do I make a tiny program that holds down left click, while toggleable through a key? I can make it toggle the leftmousebutton to click constantly but I have no clue on how to make it hold down.
void Clicker2()
{
while (1)
{
if (GetAsyncKeyState('U')) // U toggles on
{
click = true;
}
if (GetAsyncKeyState('I')) //I toggles off
{
click = false;
}
if (click == true)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
}
}

When using toggles you want to check the return value of GeyAsyncKeyState(), the least significant bit specifically, you do this by doing a bitwise AND with the result.
I have simplified your code, and converted it to using SendInput() which is what is recommended.
int main()
{
INPUT input{ 0 };
input.type = INPUT_MOUSE;
bool bClick = false;
while (true)
{
//toggles it on and off
if (GetAsyncKeyState('U') &1)
bClick = !bClick;
if (bClick)
{
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &input, sizeof(INPUT));
}
else
{
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &input, sizeof(INPUT));
}
}
return 0;
}

Related

How to loop mouse clicks if left mouse button is held down

i am trying to make an autoclicker. It should repeat MOUSEEVENTF_LEFTDOWN and MOUSEEVENTF_LEFTUP when LBUTTON is held.
Here is what i have so far. it doesn't repeat it, it double clicks.
#include <iostream>
#include<Windows.h>
using namespace std;
void menu()
{
cout << "Press 'F4' to enable and 'F5' to disable autoclicker\n";
}
void clicker()
{
bool click = false;
while (true)
{
if (GetAsyncKeyState('X'))
{
click = true;
}
else if (GetAsyncKeyState('Z'))
{
click = false;
}
if (click == true)
{
while (GetKeyState(VK_LBUTTON))
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(100);
}
}
}
}
int main()
{
menu();
clicker();
return 0;
}
Any help is appreciated! thanks
You're not giving the target application enough time to detect the key press, you should be using SendInput() as it's not deprecated.
#include <chrono>
#include <thread>
INPUT LeftButton = { 0 };
LeftButton.type = INPUT_MOUSE;
LeftButton.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &LeftButton, sizeof(INPUT));
std::this_thread::sleep_for(std::chrono::milliseconds(5));
LeftButton.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &LeftButton, sizeof(INPUT));

I'm currently making an Autoclicker and so far it's been a semi success. I need help introducing a toggle key

At the moment I'm trying to add a toggle key and make it hold to click, so that when I toggle it and hold down left click, it starts clicking. Currently it boots up and when I center the CPS it clicks, but it doesn't stop. It'll click continuously.
#include <iostream>
#include <windows.h>
using namespace std;
int x = 0, y = 0, cps;
bool click = false;
void Menu()
{
cout << "Add CPS (click per second):" << endl;
cin >> cps;
}
void Clicker()
{
while (1)
{
if (GetAsyncKeyState(VK_LBUTTON))
{
click = true;
}
if (GetAsyncKeyState(VK_RBUTTON))
{
click = false;
}
if (click == true)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
Sleep(1000 / cps);
}
}
}
int main()
{
Menu();
Clicker();
}
Please check the following code to see if it helps:
void Clicker()
{
while (1)
{
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000 && !click) //Capture that auto click start condition.
{
click = true;
}
else
{
click = false;
}
while (click)
{
if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) //Capture the stop condition.
{
break;
}
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
Sleep(1000 / cps);
}
}
}
GetAsyncKeyState Return Value:
If the most significant bit is set, the key is down.

OpenGL pointing glutSpecialFunc to member function

My OpenGL function glutSpecialFunc requires a void function pointer with 3 int parameters. This is easy to do with global functions by simply
glutSpecialFunc(processArrowKeys);
But i want to make it point to a member-function in a struct in another file like so:
inputs.h
struct Keyboard {
bool leftMouseDown;
bool keyRight, keyLeft, keyUp, keyDown;
Keyboard() : leftMouseDown(false), keyRight(false), keyLeft(false), keyUp(false), keyDown(false) {
}
void Keyboard::processArrowKeys(int key, int x, int y) {
// process key strokes
if (key == GLUT_KEY_RIGHT) {
keyRight = true;
keyLeft = false;
keyUp = false;
keyDown = false;
}
else if (key == GLUT_KEY_LEFT) {
keyRight = false;
keyLeft = true;
keyUp = false;
keyDown = false;
}
else if (key == GLUT_KEY_UP) {
keyRight = false;
keyLeft = false;
keyUp = true;
keyDown = false;
}
else if (key == GLUT_KEY_DOWN) {
keyRight = false;
keyLeft = false;
keyUp = false;
keyDown = true;
}
}
};
main.cpp
#include "inputs.h"
Keyboard keyboard;
...
int main(int argc, char **argv) {
...
// callbacks
glutDisplayFunc(displayWindow);
glutReshapeFunc(reshapeWindow);
glutIdleFunc(updateScene);
glutSpecialFunc(&keyboard.processArrowKeys); // compiler error: '&': illegal operation on bound member function expression
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMove);
glutMainLoop();
return 0;
}
Any idea how to solve this compiler error?
You can't do that directly because member functions have an implicit this pointer which has to be passed somehow through the call-chain. Instead you create an intermediary function that will forward the call to the right place:
void processArrowKeys(int key, int x, int y) {
keyboard.processArrowKeys(key, x, y);
}
int main() {
// ...
glutSpecialFunc(processArrowKeys);
// ...
}
keyboard in your code appears to be global so it is going to work. If you ever want to have a non-global state, then you will have to use a user-data pointer that some GLUT implementations support as an extension (including FreeGLUT and OpenGLUT):
void processArrowKeys(int key, int x, int y) {
Keyboard *k = (Keyboard*)glutGetWindowData();
k->processArrowKeys(key, x, y);
}
int main() {
// ...
glutSpecialFunc(processArrowKeys);
glutSetWindowData(&keyboard);
// ...
}

LNK2019 with the functions in my namespace

I have tried to read the examples of others' LNK2019 error, but they doesn't match my case. It troubled me for 2 hours and have no idea what to do.
The kbam_emu and pixel_check are the namespaces of mine. The names behind are my functions. They are declared in "kbam_emu.h", "kbam_emu.cpp", "pixel_check.h" and "pixel_check.cpp" respectively:
"kbam_emu.h"
/*
Name: keyboard and mouse emulation
Copyright: -
Author: Samuel YKC
Date: 15/11/14 20:17
Description: This header file is designed for creating bots. The essential codes for keyboard and mouse input are written by
a programmer on the Internet with the user name 'Muted'. Thanks, Muted. Here is the address for his original post:
http://forum.codecall.net/topic/45292-c-keyboardmouse-emulation/
*/
#ifndef _KBAM_EMU_H_
#define _KBAM_EMU_H_
#include <string>
#include <sstream>
#include <ctype.h>
#include <windows.h>
#include <winable.h>
using namespace std;
namespace kbam_emu
{
enum mouse_button{ left, middle, right };
void GenerateKey(int vk, BOOL bExtended);
void GenerateLine(string line);
void Click(const int X, const int Y, mouse_button button = left);
void Drag(const int X, const int Y, const int X2, const int Y2);
}
#endif
"kbam_emu.cpp"
#include <kbam_emu.h>
/* This is a function to simplify usage of sending keys */
void kbam_emu::GenerateKey(int vk, BOOL bExtended)
{
KEYBDINPUT kb = {0};
INPUT Input = {0};
/* Generate a "key down" */
if (bExtended) { kb.dwFlags = KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
/* Generate a "key up" */
ZeroMemory(&kb, sizeof(KEYBDINPUT));
ZeroMemory(&Input, sizeof(INPUT));
kb.dwFlags = KEYEVENTF_KEYUP;
if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
kb.wVk = vk;
Input.type = INPUT_KEYBOARD;
Input.ki = kb;
SendInput(1, &Input, sizeof(Input));
return;
}
/* This is a function to send a line containing English alphabets, numbers and spaces
It assumes that the user does not have the Caps Lock turned on initailly */
void kbam_emu::GenerateLine(string line)
{
stringstream is(line);
char c;
while(is.get(c))
{
if(isupper(c))
{
GenerateKey(VK_CAPITAL, TRUE); //Caps Lock on
GenerateKey(c, FALSE);
GenerateKey(VK_CAPITAL, TRUE); //Caps Lock off
}
else
{
GenerateKey(toupper(c), FALSE);
}
}
return;
}
/* This is a function to click somewhere on the screen */
void kbam_emu::Click(const int X, const int Y, mouse_button button)
{
SetCursorPos(X, Y);
DWORD down;
DWORD up;
switch(button)
{
case left:
down = MOUSEEVENTF_LEFTDOWN;
up = MOUSEEVENTF_LEFTUP;
break;
case middle:
down = MOUSEEVENTF_MIDDLEDOWN;
up = MOUSEEVENTF_MIDDLEUP;
break;
case right:
down = MOUSEEVENTF_RIGHTDOWN;
up = MOUSEEVENTF_RIGHTUP;
break;
}
INPUT Input[2];
ZeroMemory(Input, sizeof(INPUT) * 2);
Input[0].type = INPUT_MOUSE;
Input[0].mi.dwFlags = down;
Input[1].type = INPUT_MOUSE;
Input[1].mi.dwFlags = up;
SendInput(2, Input, sizeof(INPUT));
return;
}
void kbam_emu::Drag(const int X, const int Y, const int X2, const int Y2)
{
SetCursorPos(X, Y);
INPUT Input[1];
ZeroMemory(Input, sizeof(INPUT));
Input[0].type = INPUT_MOUSE;
Input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, Input, sizeof(INPUT));
SetCursorPos(X2, Y2);
INPUT Input2[1];
ZeroMemory(Input2, sizeof(INPUT));
Input2[0].type = INPUT_MOUSE;
Input2[0].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, Input2, sizeof(INPUT));
return;
}
"pixel_check.h"
/*
Name: pixel check
Copyright: -
Author: Samuel YKC
Date: 16/11/14 00:26
Description: This header file is designed for checking pixels. The essential codes for retrieving the color of a pixel
on screen are written by a programmer on the Internet with the user name 'templatetypedef'. Thanks, templatetypedef. Here
is the address for his original post:
http://stackoverflow.com/questions/4839623/getting-pixel-color-in-c
*/
#ifndef _PIXEL_CHECK_H_
#define _PIXEL_CHECK_H_
#include <windows.h>
#include "kbam_emu.h"
using namespace std;
using namespace kbam_emu;
namespace pixel_check
{
COLORREF GetPixelColor(int x, int y);
bool ComparePixelWithColor(int x, int y, COLORREF color);
void SleepOnColor(int x, int y, COLORREF color, int interval=100);
void SleepUntilColor(int x, int y, COLORREF color, int interval=100);
void ClickUntilColor(int click_x, int click_y, int pixel_x, int pixel_y, COLORREF color, int interval=1000);
}
#endif
"pixel_check.cpp"
#include "pixel_check.h"
COLORREF pixel_check::GetPixelColor(int x, int y)
{
HDC dc = GetDC(NULL);
COLORREF color = GetPixel(dc, x, y);
ReleaseDC(NULL, dc);
return color;
}
bool pixel_check::ComparePixelWithColor(int x, int y, COLORREF color)
{
HDC dc = GetDC(NULL);
COLORREF pixel_color = GetPixel(dc, x, y);
ReleaseDC(NULL, dc);
return (pixel_color==color);
}
void pixel_check::SleepOnColor(int x, int y, COLORREF color, int interval)
{
while(ComparePixelWithColor(x,y,color)) Sleep(interval);
}
void pixel_check::SleepUntilColor(int x, int y, COLORREF color, int interval)
{
while(!ComparePixelWithColor(x,y,color)) Sleep(interval);
}
void pixel_check::ClickUntilColor(int click_x, int click_y, int pixel_x, int pixel_y, COLORREF color, int interval)
{
while(!ComparePixelWithColor(pixel_x,pixel_y,color))
{
Click(click_x, click_y);
Sleep(interval);
}
}
And in my project, I put the #include like this (I placed the .h & .cpp in the default include directory so <> is fine to use):
#include <kbam_emu.h>
#include <pixel_check.h>
and uses the functions this way:
kbam_emu::Click(0, 0);
Lastly, in the .pro file I added this line:
LIBS += -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lodbc32 -lodbccp32
I supposed everything I have done is logical. Still, the linker bet a differ. I tried to post the full codes here so that I won't miss anything. Does anyone know what is wrong?
After I keep randomly trying things for days, I accidentally find the solution.
I placed my files into the project instead of the default \include directory this time. Then, I clicked the button "run qmake" in Qt and those kbam_emu.obj & pixel_check.obj that haven't been created before are finally being created. No more linker problem now!
I think this might be about the makefile is not updated automatically so I need to manually give the run qmake instruction to Qt. Hope my case would help some of you.

DirectInput Keybindings not firing

I am using this website to learn DirectX: http://www.rastertek.com/tutdx10.html I am trying to use their DirectInput tutorial to add more keybindings besides Escape. However, I cannot get my keys to fire. Here is the relevant code:
////////////////////////////////////////////////////////////////////////////////
// Filename: inputclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "inputclass.h"
InputClass::InputClass()
{
m_directInput = 0;
m_keyboard = 0;
m_mouse = 0;
}
InputClass::InputClass(const InputClass& other)
{
}
InputClass::~InputClass()
{
}
bool InputClass::Initialize(HINSTANCE hinstance, HWND hwnd, int screenWidth, int screenHeight)
{
HRESULT result;
//Store the screen sizew hcih will be used for positioning the mouse cursor.
m_screenWidth = screenWidth;
m_screenHeight = screenHeight;
//Initialize the location of the mouse on the screen.
m_mouseX = 0;
m_mouseY = 0;
//Initialize the main direct input interface.
result = DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_directInput, NULL);
if(FAILED(result))
{
return false;
}
//Initialize the direct input interface for the keyboard.
result = m_directInput->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL);
if(FAILED(result))
{
return false;
}
//Set the data format. In this case since it is a keyboard we can use the predefined data format.
result = m_keyboard->SetDataFormat(&c_dfDIKeyboard);
if(FAILED(result))
{
return false;
}
//Set the cooperative level of the keyboard to not share with other programs.
result = m_keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
if(FAILED(result))
{
return false;
}
//Now aquire the keyboard.
result = m_keyboard->Acquire();
if(FAILED(result))
{
return false;
}
//Initialize the direct input interface for the mouse.
result = m_directInput->CreateDevice(GUID_SysMouse, &m_mouse, NULL);
if(FAILED(result))
{
return false;
}
//Set the data format for the mouse using the pre-defined mouse data format.
result = m_mouse->SetDataFormat(&c_dfDIMouse);
if(FAILED(result))
{
return false;
}
//Set the cooperative level of the mouse to share with other programs.
result = m_mouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
if(FAILED(result))
{
return false;
}
//Aquire the mouse.
result = m_mouse->Acquire();
if(FAILED(result))
{
return false;
}
return true;
}
void InputClass::Shutdown()
{
//Release the mouse.
if(m_mouse)
{
m_mouse->Unacquire();
m_mouse->Release();
m_mouse = 0;
}
//Release the keyboard.
if(m_keyboard)
{
m_keyboard->Unacquire();
m_keyboard->Release();
m_keyboard = 0;
}
//Release the main interface to direct input.
if(m_directInput)
{
m_directInput->Release();
m_directInput = 0;
}
return;
}
bool InputClass::Frame()
{
bool result;
//Read the current state of the keyboard.
result = ReadKeyboard();
if(!result)
{
return false;
}
//Read the current state of the mouse.
result = ReadMouse();
if(!result)
{
return false;
}
//Process the changes in the mouse and keyboard.
ProcessInput();
return true;
}
bool InputClass::ReadKeyboard()
{
HRESULT result;
//Read the keyboard device.
result = m_keyboard->GetDeviceState(sizeof(m_keyboardState), (LPVOID)&m_keyboardState);
if(FAILED(result))
{
//If the keyboard lost focuse or was not aquired then try to get control back.
if((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
{
m_keyboard->Acquire();
}
else
{
return false;
}
}
return true;
}
bool InputClass::ReadMouse()
{
HRESULT result;
//Read the mouse device.
result = m_mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&m_mouseState);
if(FAILED(result))
{
//If the mouse lost focus or was not aqcuired then try to get control back.
if((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
{
m_mouse->Acquire();
}
else
{
return false;
}
}
return true;
}
void InputClass::ProcessInput()
{
//Update the location of the mouse cursor based on the change of the mouse location during the frame.
m_mouseX += m_mouseState.lX;
m_mouseY += m_mouseState.lY;
//Ensure the mouse location doesn't exceed the screen width or height.
if(m_mouseX < 0) {m_mouseX = 0;}
if(m_mouseY < 0) {m_mouseY = 0;}
if(m_mouseX > m_screenWidth) {m_mouseX = m_screenWidth;}
if(m_mouseY > m_screenHeight) {m_mouseY = m_screenHeight;}
return;
}
bool InputClass::IsEscapePressed()
{
//Do a bitwise and on the keyboard state to check if the escape key is currently being pressed.
std::cout << "checking ESCAPE" << std::endl;
if(m_keyboardState[DIK_ESCAPE] & 0x80)
{
std::cout << "ESCAPE" << std::endl;
return true;
}
return false;
}
bool InputClass::IsWPressed()
{
if(m_keyboardState[DIK_W] & 0x80)
{
std::cout << "W" << std::endl;
return true;
}
return false;
}
bool InputClass::IsAPressed()
{
if(m_keyboardState[DIK_A] & 0x80)
{
return true;
}
return false;
}
bool InputClass::IsSPressed()
{
if(m_keyboardState[DIK_S] & 0x80)
{
return true;
}
return false;
}
bool InputClass::IsDPressed()
{
if(m_keyboardState[DIK_D] & 0x80)
{
return true;
}
return false;
}
void InputClass::GetMouseLocation(int& mouseX, int& mouseY)
{
mouseX = m_mouseX;
mouseY = m_mouseY;
return;
}
Other file:
////////////////////////////////////////////////////////////////////////////////
// Filename: systemclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "systemclass.h"
SystemClass::SystemClass()
{
m_Input = 0;
m_Graphics = 0;
}
SystemClass::SystemClass(const SystemClass& other)
{
}
SystemClass::~SystemClass()
{
}
bool SystemClass::Initialize()
{
int screenWidth, screenHeight;
bool result;
// Initialize the width and height of the screen to zero before sending the variables into the function.
screenWidth = 0;
screenHeight = 0;
// Initialize the windows api.
InitializeWindows(screenWidth, screenHeight);
// Create the input object. This object will be used to handle reading the keyboard input from the user.
m_Input = new InputClass;
if(!m_Input)
{
return false;
}
// Initialize the input object.
result = m_Input->Initialize(m_hinstance, m_hwnd, screenWidth, screenHeight);
if(!result)
{
MessageBox(m_hwnd, L"Could not initialize the input object.", L"Error", MB_OK);
return false;
}
// Create the graphics object. This object will handle rendering all the graphics for this application.
m_Graphics = new GraphicsClass;
if(!m_Graphics)
{
return false;
}
// Initialize the graphics object.
result = m_Graphics->Initialize(screenWidth, screenHeight, m_hwnd);
if(!result)
{
return false;
}
return true;
}
void SystemClass::Shutdown()
{
// Release the graphics object.
if(m_Graphics)
{
m_Graphics->Shutdown();
delete m_Graphics;
m_Graphics = 0;
}
// Release the input object.
if(m_Input)
{
m_Input->Shutdown();
delete m_Input;
m_Input = 0;
}
// Shutdown the window.
ShutdownWindows();
return;
}
void SystemClass::Run()
{
MSG msg;
bool done, result;
// Initialize the message structure.
ZeroMemory(&msg, sizeof(MSG));
// Loop until there is a quit message from the window or the user.
done = false;
while(!done)
{
// Handle the windows messages.
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// If windows signals to end the application then exit out.
if(msg.message == WM_QUIT)
{
done = true;
}
else
{
// Otherwise do the frame processing.
result = Frame();
if(!result)
{
done = true;
}
}
//Check if the user pressed escape and wants to quit.
if(m_Input->IsEscapePressed() == true)
{
std::cout << "ESCAPE" << std::endl;
done = true;
}
if(m_Input->IsWPressed() == true)
{
m_Graphics->SetCameraPos(0, 1, 0);
std::cout << "W" << std::endl;
}
if(m_Input->IsAPressed() == true)
{
m_Graphics->SetCameraPos(1, 0, 0);
std::cout << "A" << std::endl;
}
if(m_Input->IsSPressed() == true)
{
m_Graphics->SetCameraPos(0, -1, 0);
std::cout << "S" << std::endl;
}
if(m_Input->IsDPressed() == true)
{
m_Graphics->SetCameraPos(-1, 0, 0);
std::cout << "D" << std::endl;
}
}
return;
}
bool SystemClass::Frame()
{
bool result;
int mouseX, mouseY;
//Do the frame processing.
result = m_Input->Frame();
if(!result)
{
return false;
}
//Get the location of the mouse from the input object.
m_Input->GetMouseLocation(mouseX, mouseY);
// Do the frame processing for the graphics object.
result = m_Graphics->Frame(mouseX, mouseY);
if(!result)
{
return false;
}
result = m_Graphics->Render();
if(!result)
{
return false;
}
return true;
}
LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
return DefWindowProc(hwnd, umsg, wparam, lparam);
}
void SystemClass::InitializeWindows(int& screenWidth, int& screenHeight)
{
WNDCLASSEX wc;
DEVMODE dmScreenSettings;
int posX, posY;
// Get an external pointer to this object.
ApplicationHandle = this;
// Get the instance of this application.
m_hinstance = GetModuleHandle(NULL);
// Give the application a name.
m_applicationName = L"Engine";
// Setup the windows class with default settings.
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = m_hinstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hIconSm = wc.hIcon;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = m_applicationName;
wc.cbSize = sizeof(WNDCLASSEX);
// Register the window class.
RegisterClassEx(&wc);
// Determine the resolution of the clients desktop screen.
screenWidth = GetSystemMetrics(SM_CXSCREEN);
screenHeight = GetSystemMetrics(SM_CYSCREEN);
// Setup the screen settings depending on whether it is running in full screen or in windowed mode.
if(FULL_SCREEN)
{
// If full screen set the screen to maximum size of the users desktop and 32bit.
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = (unsigned long)screenWidth;
dmScreenSettings.dmPelsHeight = (unsigned long)screenHeight;
dmScreenSettings.dmBitsPerPel = 32;
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
// Change the display settings to full screen.
ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
// Set the position of the window to the top left corner.
posX = posY = 0;
}
else
{
// If windowed then set it to 800x600 resolution.
screenWidth = 800;
screenHeight = 600;
// Place the window in the middle of the screen.
posX = (GetSystemMetrics(SM_CXSCREEN) - screenWidth) / 2;
posY = (GetSystemMetrics(SM_CYSCREEN) - screenHeight) / 2;
}
// Create the window with the screen settings and get the handle to it.
m_hwnd = CreateWindowEx(WS_EX_APPWINDOW, m_applicationName, m_applicationName,
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP,
posX, posY, screenWidth, screenHeight, NULL, NULL, m_hinstance, NULL);
// Bring the window up on the screen and set it as main focus.
ShowWindow(m_hwnd, SW_SHOW);
SetForegroundWindow(m_hwnd);
SetFocus(m_hwnd);
// Hide the mouse cursor.
ShowCursor(false);
return;
}
void SystemClass::ShutdownWindows()
{
// Show the mouse cursor.
ShowCursor(true);
// Fix the display settings if leaving full screen mode.
if(FULL_SCREEN)
{
ChangeDisplaySettings(NULL, 0);
}
// Remove the window.
DestroyWindow(m_hwnd);
m_hwnd = NULL;
// Remove the application instance.
UnregisterClass(m_applicationName, m_hinstance);
m_hinstance = NULL;
// Release the pointer to this class.
ApplicationHandle = NULL;
return;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam)
{
switch(umessage)
{
// Check if the window is being destroyed.
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
// Check if the window is being closed.
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}
// All other messages pass to the message handler in the system class.
default:
{
return ApplicationHandle->MessageHandler(hwnd, umessage, wparam, lparam);
}
}
}
Why don't try with GetAsyncKeyState function?
you can put that in your update function and check for the desired key code.
Something like this:
//check if num1 is pressed, if yes switch off the lights.
if(GetAsyncKeyState('1') & 0x8000)
{
mLightCount = 0;
}
As it turned out, the keybindings were working. I tried switching the close key to w and pressing W closed the window. What was wrong was how I was moving the camera. Thanks for helping!