Thread: Where is the error?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    184

    Where is the error?

    Can somebody tell me why when I run this code, I do not get an WM_KEYDOWN message when I press a key????????

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <time.h>
    
    LRESULT CALLBACK Callback(HWND, UINT, WPARAM, LPARAM);
    static char *WindowClassName = "MyHack";
    
    int main(void) {
    	WNDCLASS Window;
    	HWND hack;
    	MSG messages;
    
    	Window.style = CS_GLOBALCLASS;
    	Window.lpfnWndProc = Callback;
    	Window.cbClsExtra = 0;
    	Window.cbWndExtra = 0;
    	Window.hInstance = 0;
    	Window.hIcon = NULL;
    	Window.hCursor = NULL;
    	Window.hbrBackground = NULL;
    	Window.lpszMenuName = NULL;
    	Window.lpszClassName = WindowClassName;
    
    	if(!RegisterClass(&Window)) {
    		fprintf(stderr, "Could not register the window");
    		return 1;
    	}
    
    	hack = CreateWindow(WindowClassName, "", WS_OVERLAPPED | WS_SYSMENU, 0, 0, 0, 0, HWND_DESKTOP, NULL, 0, NULL);
    	if(!hack) {
    		fprintf(stderr, "Could not create the window");
    		return 1;
    	}
    
    	while (GetMessage (&messages, NULL, 0, 0)) {
    	printf("got message");
    		TranslateMessage(&messages);
    		DispatchMessage(&messages);
    	}
    
    	return messages.wParam;
    }
    
    LRESULT CALLBACK Callback(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
    	char temp[256];
    	switch(message) {
    		case WM_CREATE:
    		printf("\n\nhit create\n\n");
    			SetTimer(hwnd, 10000, 5000, NULL);
    			break;
    		case WM_TIMER:
    		printf("\n\nhit timer\n\n");
    			//this is where your stuff would go.
    			//printf("\r%ld", time(NULL));
    			break;
    		case WM_KEYDOWN:
    		printf("\n\nKeyDown\n\n");
    		break;
    		case WM_KEYUP:
       			printf("\n\nKey has been pressed\n\n");
    			break;
    		case WM_DESTROY:
       			PostQuitMessage(0);
       			break;
    	}
    	return DefWindowProc(hwnd, message, wparam, lparam);
    }
    Thanks in advance for any and all help
    Kendal

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    TranslateMessage() is turning WM_KEYDOWN into WM_CHAR.

    http://msdn.microsoft.com/library/de...es/WM_CHAR.asp
    Away.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Where is the error?
    It should read "Where ISN'T the error"

    Are you trying to create a console app or a windows app? If you are check out adrianxw's tutorial. Sorry don't have the link atm.

    Edit: Found link http://www.adrianxw.dk/SoftwareSite/index.html

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    I have read Adrianxw's tutorial. I am currently using the readconsoleinput from his tutorial. The problem is I have to use a timer. In order to use SetTimer, you have to use TranslateMessage and DispatchMessage. When I use both of those, I have problems. So I have been testing different approaches. With the code above, I have a printf test line directly after the GetMessage routine. That printf line is only executed when the timer goes off. If I click the mouse, move the mouse, or press keys on the keyboard, no message is retrieved. Why is that??????????????? Besides, the code above was downloaded from one of the posts on the CProgramming forum. I don't remember the specific post, but I downloaded it and made a few changes. I just can't get GetMessage to retrieve anything but the WM_TIMER message. Any suggestions??????????

    Thanks,
    Kendal

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The reason that you aren't getting any messages is because the window isn't there to take input.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    Good point!
    Do you know of any way I can use readconsoleinput and some sort of timer to, for instance, interrupt readconsoleinput every 30 seconds, so that I can execute a routine?????????????????

    Thanks,
    Kendal

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    	while (GetMessage (&messages, NULL, 0, 0)) {
    		printf("got message");
    		if (IsDialogMessage(hack,&messages))
    		;
    		else
    		{
    			TranslateMessage(&messages);
    			DispatchMessage(&messages);
    		}
    	}

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The problem you have with ReadConsoleInput() is that it is a blocking call. If you remember from the tutorial, I used it to stop something looping all the time. It waits for an interrupt.

    I do not have the source for windows, but I suspect, if I was doing it, I would queue an waitable event on the keyboard line and another on the mouse lines then use WaitForMultipleItems() with the appropriate handles. Since the timer handle isn't on the list, it will not break the wait when it comes in.

    I had a look at this earlier this week, (I have very little time this week), and tried, for example to subclass the console window, insert my own windows message handler before the default windows handler and trap WM_TIMER when it came in so I could use SendInput() to force a "keypress", (breaking the wait), but it didn't come in, leading me to suspect that the task is actually in a WIO/SWIO state, thus not schedulable.

    When I get a minute, I'll stick a low level tracer on it to be sure. There will be other ways of doing this.

    Kendal:

    You still need this stuff to run with your old crap right and DJGPP? I noticed you playing with some Windows code up there - this, of course, would make a great deal of difference - piece of cake in a GUI app.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    Adrianxw,

    We are coding in DevC++ as .c programs. This way we can still keep all the previous code written in DJGPP with minimum changes. As for the problem at hand, I think we have found a way out. If you remember, one of the main concerns was CPU usage. The answer was right in front of me in your tutorial. Somebody had to make the suggestion to me, but it was right there. Sleep(1). That frees up our CPU. The other problem, the SetTimer, has actually been replaced. Since it has to use the GetMessage, TranslateMessage, DispatchMessage routines, SetTimer has been dropped. My administrator has actually written a .c file that actually works as a timer, and it does not have to use any of the messages. It can be used in a true console app. So for now, we have the problem solved. How long we stay that way, I don't know. I know this though, we will run into many more problems. But hey, that's life. Thanks greatly for all of the help. If I can help in any way, let me know.

    Thanks a million,
    Kendal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM