KEY_PRESSED error

This is a discussion on KEY_PRESSED error within the Game Programming forums, part of the General Programming Boards category; Code: if (KEY_DOWN(VK_ESCAPR)) { PostMessage(main_window_handle, WM_DESTROY,0,0); } I get an error that says KEY_DOWN and main_window_handle are not declared. Are ...

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    KEY_PRESSED error

    Code:
    if (KEY_DOWN(VK_ESCAPR))
      {
      PostMessage(main_window_handle, WM_DESTROY,0,0);
      }
    I get an error that says KEY_DOWN and main_window_handle are not declared. Are they suppost to be???
    Your mom is like a struct, she has no class

    How many C programmers does it take to screw in a light bulb? One to do it, 99 to tell him how to do it faster.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Got any other code or is that it?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes! Define them...

    Code:
    #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
    
    HWND main_window_handle; //make sure you set this to hwnd passed to WinMain later...
    Code:
    if (KEY_DOWN(VK_ESCAPR))
    //wrong
    
    if (KEY_DOWN(VK_ESCAPE))
     //right
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Code:
    //make sure you set this to hwnd passed to WinMain later...
    so how would I do that? Thoe following is my WinMain

    Code:
    int WINAPI WinMain(HINSTANCE hinstance,
                                   HINSTANCE hprevinstance,
    	               LPSTR lpcmdline,
    	               int ncmdshow)
    {
    
    WNDCLASSEX winclass; // this will hold the class we create
    HWND	   hwnd;	 // generic window handle
    MSG		   msg;// generic message
    
    // first fill in the window class stucture
    winclass.cbSize         = sizeof(WNDCLASSEX);
    winclass.style			= CS_DBLCLKS | CS_OWNDC | 
                              CS_HREDRAW | CS_VREDRAW;
    winclass.lpfnWndProc	= WindowProc;
    winclass.cbClsExtra		= 0;
    winclass.cbWndExtra		= 0;
    winclass.hInstance		= hinstance;
    winclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
    winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
    winclass.hbrBackground	= (HBRUSH)GetStockObject(DKGRAY_BRUSH);
    winclass.lpszMenuName	= NULL;
    winclass.lpszClassName	= WINDOW_CLASS_NAME;
    winclass.hIconSm        = LoadIcon(NULL, "G3D.ICO");
    
    // register the window class
    if (!RegisterClassEx(&winclass))
    	return(0);
    
    // create the window
    if (!(hwnd = CreateWindowEx(0, // extended style
                                WINDOW_CLASS_NAME,   // class
    						    "Geo 3D Game Engine V1.0 Alpha -- By Ryan ****** ", // title
    						    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    					 	    0,0,	    // initial x,y
    						    800,600,  // initial width, height
    						    NULL,	    // handle to parent 
    						    NULL,	    // handle to menu
    						    hinstance,// instance of this application
    						    NULL)))	// extra creation parms
    return(0);
    
    
    //Calls Game Initilizastion
    int Game_Init();
    
    
    
    // enter main event loop
    while(1)
      {
      if (PeekMessage(&msg, NULL,0,0,PM_REMOVE))
          {
          //Test if this is a quit.
          if (msg.message == WM_QUIT)
          break;
          
          //Translate any accelerator keys
          TranslateMessage(&msg);
          
          //Send message to window proc.
          DispatchMessage(&msg);
          } //end IF(PeekMessage);
    
    
    
    
    // Calls Game_Shutdown
    int Game_Shutdown();
    }// end while(1);
    
    //Call Game_Shutdown
    int Game_Shutdown();
    
    //Return to windows
    return(msg.wParam);
    
    }//end WinMain
    Your mom is like a struct, she has no class

    How many C programmers does it take to screw in a light bulb? One to do it, 99 to tell him how to do it faster.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Where is the code that causes the error?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    look at Jawibs post above.



    ~Trooper
    Your mom is like a struct, she has no class

    How many C programmers does it take to screw in a light bulb? One to do it, 99 to tell him how to do it faster.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    No, I mean, you haven't actually posted up the code and it's context that actually causes the error. Posting up a few lines isn't enough, I need to know what function it's in and what other code is there. Just post your entire code.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    okay here's the .cpp file


    ~Trooper
    Attached Files Attached Files
    Your mom is like a struct, she has no class

    How many C programmers does it take to screw in a light bulb? One to do it, 99 to tell him how to do it faster.

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    case WM_CREATE: 
            {
    	// do initialization stuff here
    	main_window_handle=hwnd;
                    return(0);
            }
    That's where I'd put it. I think WM_CREATE is just about the earliest place you can put the code, except maybe WM_NCCREATE.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    if (!(hwnd = CreateWindowEx(0, // extended style
                                WINDOW_CLASS_NAME,   // class
    						    "Geo 3D Game Engine V1.0 Alpha -- By Ryan ****** ", // title
    						    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    					 	    0,0,	    // initial x,y
    						    800,600,  // initial width, height
    						    NULL,	    // handle to parent 
    						    NULL,	    // handle to menu
    						    hinstance,// instance of this application
    						    NULL)))	// extra creation parms
    return(0);
    You could probably put it right after you create the window
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Placing it in the WM_NCCREATE handler would come before that, Jawib. A lot happens before CreateWindowEx() returns.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    But that would be even before the message pump begins, handling your WM_NCCREATE message. Unless CreateWindowEx() calls your message handler on its own?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Ummmmmmmmmm.........

    you could put it here

    if (!(main_window_handle = CreateWindowEx(0, WINDOW_CLASS_NAME,
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  14. #14
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    That's probably the best place to put it really. If you also initialize the variable with NULL in the first place, then you can be assured that whenever you use the handle, it will either point to a window that has been created and is valid, or it will be NULL, in which case it'll just get flagged as invalid. If you were to place the assignment in WM_NCCREATE, and by chance some function used the handle before WM_CREATE returned, you might have some problems.

    So I guess novacain is right. Do the following:

    Code:
    //at the top of the file
    HWND main_window_handle=NULL; 
    .
    .
    .
    int WINAPI WinApi(...)
    {
    .
    .
    .
    main_window_handle=CreateWindowEx(...);
    if (!main_window_handle)
           exit(1);
    This way, main_window_handle does not have the window handle until WM_CREATE returns, which is a good thing IMO.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

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, 09:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21