Fast and Good Keyboard/Mouse Test without the message handler

CK CK 3788 
Created at  
985 0 0
In this short snippet, I will show you a Win32 function that checks whether a button is pressed or not. The good thing is that you don't need to handle the WM_KEYDOWN message. The second advantage is that it is really easy to use and that it can be used everywhere in the code. Here is the function-definition:

SHORT GetAsyncKeyState(
   int vKey // virtual-key code
);


vKey is the virtual key code of the keyboard or mouse button you want to test. GetAsyncKeyState(..) returns true if this button is pressed and false if not. I always use GetAsyncKeyState(..) for my games and demos because it can be used simply like this without a long message handle. Here is a little example how to use it:

void CheckKeyboard()
{
   //Escape is down
   if (GetAsyncKeyState(VK_ESCAPE))
   {
      PostMessage(hWnd,WM_CLOSE,0,0);
   }
 
   //Right mouse is down
   if (GetAsyncKeyState(VK_RBUTTON))
   {
      //..
      //Do something
      //..
   }
}


In this function, we will check what keys are down. You could now use this function in your main loop or somewhere else in your code. Here is a list of the most important virtual key codes:










































Virtual Key Code

Corresponding key on the keyboard

VK_ESCAPE

Escape

VK_SPACE

Space

VK_LEFT

Left Arrow

VK_RIGHT

Right Arrow

VK_UP

Up Arrow

VK_DOWN

Down Arrow

VK_SHIFT

Shift

VK_CONTROL

Control

VK_LBUTTON

Left Mouse Button

VK_RBUTTON

Right Mouse Button

Tags C++ GetAsyncKeyState PostMessage VK_DOWN VK_ESCAPE VK_LEFT VK_RBUTTON VK_RIGHT VK_SHIFT VK_SPACE VK_UP Facebook X
Comments 0
Similar posts
  1. Simple CGI Programming in C
    550
  2. C++ CGI Example working on Apache
    490
  3. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    473
  4. MFC based World Wide Web HTTP Server Source Code
    462
  5. Double Linked List based in C++
    799
  6. Binary Search Sample Code
    409
  7. How to run shell command by MFC ?
    441
  8. How to Close Another Application by Windows Caption
    392
  9. How to search file on certain directory ?
    633
  10. How to call SetTimer function on MFC CDialog class ?
    652
  11. Makefile to automate the build process for C++
    285
  12. How to load HTML resource on MFC ?
    848
  13. URL Encode, Decode function for MFC
    608
  14. KMP String Matching Algorithm
    676
  1. How to restrict a program to a single instance
    732
  2. Compress and decompress strings in C#
    975
  3. Graphics in C# - Irregular Forms
    914
  4. Extracting the Country from IP Address
    471
  5. String Replace Function For C#
    1,559
  6. How to print character in C# by ASCII code
    479
  7. UDP Send and Receive Using CAsyncSocket
    1,206
  8. Using the shell to receive notification of removable media being inserted or removed
    681
  9. Adding Custom Paper Sizes To Named Printers
    651
  10. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    972
  11. A Customizable Printing Text Class
    439
  12. Simplified .NET Printing In C#
    696
  13. How To Print Text In C#
    456
  14. C# Lists
    1,162
  15. Simple Example of IF/THEN using C#
    1,270
  16. Simple C# CGI working on Apache
    486
  17. How to send binary data through C# CGI app?
    998
  18. Interacting With TinyPic From C#
    1,314
  19. Creating A Watched Folder With Assigned Events
    1,672
  20. Calling Your Main Thread From A Worker Thread In C#
    551
  21. HashTable Tutorial In C#
    1,794
  22. Stack Tutorial In C#
    674
  23. Queue Tutorial In C#
    1,753
  24. Create PDF Files On the Fly in C#
    1,009
  25. How to call Visual C/C++ implemented DLL functions in C# - Simple DLLImport
    1,174
  26. Send Email Using C#
    1,381
  27. How To Read And Write A Cookie
    365
  28. Using C# With Cookies
    629
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    29
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    55
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    49
  4. How do I create animated background for Google Meet?
    22,888
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    271
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    103
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    168
  8. Array of arguments passed to script in PHP
    97
  9. Invalid Auction Storms in Ad Tech
    106
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    207
  11. The Two Clocks of Ad Server Architecture
    77
  12. Frequency Cap Architecture Overview
    526
  13. Collecting Proof-of-Play from 100,000 Screens
    152
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    302
  15. What is RedisVL?
    114
  16. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    246
  17. TPU & GPU Cluster Management Tools
    257
  18. The Concept of Scaffolding in AI Agents
    244
  19. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    596
  20. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    267
  21. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    353
  22. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    206
  23. Machine Learning: Training vs. Ranking
    229
  24. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  25. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    202
  26. Designing a Data Curation System Around a Vector Database
    230
  27. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  28. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    671
  29. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    200
  30. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    232