Thread: Mouse being controlled my arrow keys

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    Mouse being controlled my arrow keys

    Hi,

    I'm trying to write a code where I can use the arrow keys to move the mouse cursor. I've written my own code, but it doesn't seem to work.

    Can someone show me what my problem is?

    Here's my code:
    Code:
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HDC	hdc;
    	PAINTSTRUCT ps ;
    	static RECT rect;
    	static int cxClient; 
    	static int cyClient; 
    	HCURSOR hCursor ;
    	
    	switch (message)
    	{
    		case WM_CREATE:
    			return 0;
    
    		case WM_KEYDOWN:
    			switch (wParam)
    			 {
    				 case VK_SHIFT:
    					 SendMessage (hwnd, WM_LBUTTONDOWN, wParam, lParam) ;
    					 return 0;
    				 case VK_LEFT:
    					 SendMessage (hwnd, WM_MOUSEMOVE, wParam, lParam);
    					 return 0;
    				 case VK_RIGHT:
    					 SendMessage (hwnd, WM_MOUSEMOVE, wParam, lParam);
    					 return 0;
    				 case VK_UP:
    					 SendMessage (hwnd, WM_MOUSEMOVE, wParam, lParam);
    					 return 0;
    				 case VK_DOWN:
    					 SendMessage (hwnd, WM_MOUSEMOVE, wParam, lParam);
    					 return 0;
    				 default:
    					 return 0;
    			 }
    			return 0;
    
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    			SetWindowExtEx(hdc, 1600, 1200, NULL);
    			SetViewportExtEx (hdc, cxClient, cyClient, NULL) ;
    			EndPaint(hwnd, &ps);
    			return 0;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    	}
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Can we see the WM_MOUSEMOVE message?

    I would think within the WM_MOUSEMOVE message you'd need some if statements to determine the keys that are being pressed, and move the mouse according to that.

    EDIT: Add the WM_MOUSEMOVE message to your WndProc(), and do what I said above. Also look in to simulating mouse/keyboard events on msdn.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    Arrite thanks for the advice, I think I have something working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Movement with arrow keys
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 02-06-2005, 04:35 PM
  2. Interfacing with arrow keys...
    By adityakarnad in forum Game Programming
    Replies: 1
    Last Post: 08-30-2003, 10:25 PM
  3. Ascii code for arrow keys
    By beginner in forum C Programming
    Replies: 1
    Last Post: 11-07-2002, 01:29 PM
  4. msdos arrow keys?
    By seditee in forum Game Programming
    Replies: 3
    Last Post: 05-07-2002, 11:29 PM
  5. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 11:49 AM