Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getkey?...recognize key depression

Status
Not open for further replies.

AndBack3

Programmer
Oct 27, 2001
15
0
0
US
Does anyone know how to get any key pressed by a user?

ie. in a loop, if user presses
, a variable is incremented, if a user presses,
, a variable is decremented. I also want this loop to continue running whether the user presses a button or not.​
 
I'll give you two function : getc(), getch().

If you need more help there are some example in other thread. Try a search with keyboard in C forums.
 
with Getc or Getch wouldnt the program wait untill you would have pressed a key?

i think thats not what he wants, heh. at least thats not what i want :) i need to let the program run (like the main_loop)

tnx in advance,
Deathwish
 
Here I am.
I hope you can use kbhit(). It doesn't work on UNIX, LINUX and ANSI.

See this simple program which print the code of the Key you pressed.


#include <stdio.h>
#include <conio.h>

int main()
{
char key=0;

while( key != 27 )
{
while( !kbhit() )
{
}
key = getch();
printf(&quot;%d&quot;,key);
}
return 0;
}
 
thx for pointing out the kbhit () function

looked around and found out exactly how to use it....for some reason i keep getting the error :implicit declaration of function `int kbhit(...)'


what this means, i have no idea, but i get it when i make my own program, copy yours, or copy others
 
There is only 1 little drawback of Kbhit():

This comes directly from the c++ builder help
Note: Do not use this function for Win32s or Win32 GUI applications.

so u cant use it for windows based programs.
Is there a way around this??
 
I agree, kbhit() work only in DOS or command application.
So my previous example work on Win9x an NT4.0 .

But if you're application is within a window, you'll have to learn more about Virtual Key and WNDPROC callback function.

Here is the same example but in a window format :

Code:
//-----------------------------------------------------------------------------
#define APPNAME  &quot;GetKey2&quot;
#define APPTITLE &quot;GetKey2&quot;
#include <windows.h>
#include <stdio.h>
#include <time.h>

HWND hWnd;

/* Print the virtual Key Code */
void PrintKey(WPARAM wParam)
{
  HDC pDc;
  RECT rct;
  char msg[10];
  
  sprintf(msg, &quot;%d\n&quot;, wParam );
  pDc = GetDC( hWnd );
  GetClientRect( hWnd, &rct );

  FillRect(pDc, &rct, (HBRUSH)GetStockObject(BLACK_BRUSH)); 

  DrawText( pDc, msg, strlen(msg), &rct, DT_LEFT	| DT_TOP);
 
  ReleaseDC(hWnd,pDc);
}

LRESULT CALLBACK WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_KEYDOWN:
    PrintKey( wParam );
 	  break;

	case WM_DESTROY:
		PostQuitMessage (0);
		break;
	};
	return DefWindowProc(hWnd, message, wParam, lParam);
};

bool WindowInit (HINSTANCE hThisInst, int nCmdShow)
{
	WNDCLASSEX		    wcl;
	
	/* define the class of your main window */
  wcl.cbSize = sizeof(WNDCLASSEX);
	wcl.hInstance		= hThisInst;
	wcl.lpszClassName	= APPNAME;
	wcl.lpfnWndProc		= WindowProc;
	wcl.style			= 0;
	wcl.hIcon			= LoadIcon (hThisInst, IDC_ICON);
	wcl.hIconSm = LoadIcon (hThisInst, IDC_ICON);
	wcl.hCursor			= LoadCursor (hThisInst, IDC_ARROW);
	wcl.lpszMenuName	= NULL;
	wcl.cbClsExtra		= 0;
	wcl.cbWndExtra		= 0;
	wcl.hbrBackground	= (HBRUSH) GetStockObject (BLACK_BRUSH);

  /* Register class in Windows system */
	RegisterClassEx (&wcl);

  /* Create the window in memory */
	hWnd = CreateWindowEx( WS_EX_TOPMOST, APPNAME, APPTITLE, WS_OVERLAPPEDWINDOW, 75, 75, 320, 240, NULL, NULL, hThisInst, NULL);

	if(!hWnd) return false;
  
	return true;
};

bool AppInit (HINSTANCE hThisInst, int nCmdShow)
{
  if(!WindowInit (hThisInst, nCmdShow)) return false;

	ShowWindow (hWnd, nCmdShow);
	UpdateWindow (hWnd);

	return true;
};

int APIENTRY WinMain (HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;
	if(!AppInit (hThisInst, nCmdShow)) return false;
	
	while (1)
	{
	  // Look if there are messages
		if(PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
		{
		  // Get the first message in the stack
			if(!GetMessage (&msg, NULL, 0, 0)) break;
			// Convert the message for WNDPROC functions
			TranslateMessage (&msg); 
			// Give the message to all applications
			DispatchMessage (&msg);
		}
		else
  	{
			// Do what you want when no message happens  
		}
	};

	return 0;
};

Hope I helped you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top