i am new to c++ so please dont throw any crazy words around :D

im using dev C++ form bloodshed as my editor / compiler

what i am trying to do is when a user hits one of the special buttons all keyboard input after that will be reformated depending
on which button he has click im just not sure where to go from here there's what i have so far

#include <iostream>  
#include <windows.h> 

using namespace std;    


int dostuff (int key_stroke);
//===============================================================================================================================
int main(void)  
{
 char i; 
      while (1) { for(i = 8; i <= 190; i++) if (GetAsyncKeyState(i) == -32767){dostuff (i); }}
   cin.get( ); //make system hold
return 0;
}
//===============================================================================================================================

int dostuff (int key_stroke)
{
    if (key_stroke == 45){cout << "i do stuff 4 a_";}// insert button on keyboard
    if (key_stroke == 36){cout << "i do stuff 4 b_";}// home button on keyboard
    if (key_stroke == 33){cout << "i do stuff 4 c_";}// page up button on keyboard
    cout << key_stroke << endl; // just echo what button is pressed
}

Recommended Answers

All 5 Replies

Really not the best way to detect keystrokes, I'd suggest a Windows hook, because you can actually change what the output of that key will be. For example, here's how you could turn the Space bar into the Return Key.

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
using namespace std;

LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
	PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
  if (wParam == WM_KEYDOWN) {
		switch (p->vkCode) {
      case VK_SPACE:
        {
          // Space pressed

          // Press ENTER instead
          keybd_event( VK_RETURN, 0, 0, 0 );

          // Dont let windows process SPACE
          return 1;
        }
        break;
    }
  }
  return CallNextHookEx(NULL, nCode, wParam, lParam);
}

int main() {
  // Set windows hook
  HHOOK keyboardHook = SetWindowsHookEx(
      WH_KEYBOARD_LL,
      keyboardHookProc,
      GetModuleHandle( 0 ),
      0);

  MSG msg;
  while ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
  }
}

Hope this helps.

Misread the question, but that's still a great way to detect keypresses, the rest shouldn't be hard.

wow that really was a better way of doing it i have another question for you now how do i modify what goes to the keyboard this is for a barcode scanner so if one option is pressed and the data input after that would have to be modified somehow this is what i have so far any help would be greatly appreciated

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
using namespace std;
int whichbuttonpressed;

LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
	PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
  if (wParam == WM_KEYDOWN) {
		switch (p->vkCode) {
//===============================================================================================================
//  do  some things when the insert button is pressed                                                         
//===============================================================================================================      
case VK_INSERT: {
     whichbuttonpressed = 1;
      cout << "hey you this is insert \n\n";
      
INPUT input[1];
memset(input, 0, sizeof(input));
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = 65;// A
input[0].ki.dwFlags = 0;
input[0].ki.time = 0;
input[0].ki.dwExtraInfo = 0;


SendInput(1,input,sizeof(INPUT));
           return 1;
             }
//===============================================================================================================
//  do  some things when the home button is pressed                                                         
//===============================================================================================================       
case VK_HOME:{ 
     whichbuttonpressed = 2;
     cout << "hey you this is home \n\n";
           return 1;
           }

//===============================================================================================================
//  do  some things when the page up button is pressed                                                         
//=============================================================================================================== 
case VK_PRIOR:{
     whichbuttonpressed = 3;   
     cout << "hey you this is page up \n\n";
          return 1;
          }
          
//===============================================================================================================
//  this is the end og buttons                                                        
//===============================================================================================================         
      break;
    }
  }
  return CallNextHookEx(NULL, nCode, wParam, lParam);
}

int main(void) {
  // Set windows hook
  HHOOK keyboardHook = SetWindowsHookEx(
      WH_KEYBOARD_LL,
      keyboardHookProc,
      GetModuleHandle( 0 ),
      0);

  MSG msg;
  while ( GetMessage(&msg, NULL, 0, 0) > 0 ) {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
  }
}

So you want to swap keys around, for example the user presses 'INSERT', 'A' is pressed instead? You don't need to use SendInput, it's much simpler to use keybd_event, so you can change that case to:

case VK_INSERT:
  {
      whichbuttonpressed = 1;
      cout << "hey you this is insert \n\n";
      keybd_event( 'A',0,0,0 );
      return 1;
  }

Other than that, I'm not entirely sure what it is you're asking for.

kinda but what i am trying to do is if you hit one of the three buttons it will turn on certian things so when the next item is scanned it will take out spaces or drop just the first letter

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.