Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

JMouse - Win32 C Jump Mouse Program

linked list

  • Please log in to reply
No replies to this topic

#1 RhetoricalRuvim


    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 15 December 2011 - 06:38 PM

JMouse - where J stands for 'Jump' - is a program designed for uses where moving the mouse back and forth is a requirement, and the user is too lazy (:)) to do the laborious job, or maybe the user just needs more speed at moving the mouse. Whatever the reason, this program is meant to help by hosting keyboard shortcuts that make the mouse jump to the appropriate position on the screen.

Here is (/are) the built-in keyboard shortcut(/s) for this program:
  • Ctrl + Alt + E Exit the JMouse program.

You can alter the shortcut('s/s') values by modifying the 'c_exit' #define(/s).

Aside from the built-in keyboard shortcut(/s) (hotkey(/s)), there are also jump hotkeys. These are the hotkeys (keyboard shortcuts) that you use to make the mouse jump to the position you want.

The way it works
When you start this program, you include - as part of the command line, right after the path to the program - a white-space-separated list of screen coordinates.

During the initialization, the program looks at the list of coordinates and registers a hotkey for each coordinate. When you use one of the registered hotkeys, the program makes the mouse jump to the position indicated by the corresponding command line coordinate pair.

A little more elaborate explanation
When the program starts, the program scans the command line, starting from the point right after the path to itself. It makes a linked list - a bunch of structures that have values and point to each other - for organizing all the coordinates and hotkey IDs. When there aren't any more coordinates left from the command line to process, the program adds another pair of the coordinates (0, 0), and returns from scanning the command line to actually doing things.

Henceforth, the program uses the linked list it made to navigate through the coordinates, determine which position to move the mouse to based on the hotkey ID, etc.

In making the linked list, this program allocates a piece of memory for each structure in the list. Each structure has an ID integer, a nested value POINT structure, and pointers to the first, previous, and next structures in the chain. Upon exiting, JMouse executes the finalization of its environment by freeing all the pieces of memory it allocated for the linked list.

An example
Here's an example command line:
jmouse (1, 2) (3,4) (5, 6)

When you run the above command line, the following keyboard shortcuts are assigned:
  • Ctrl + Alt + E Built-in; the exit hotkey.
  • Ctrl + Alt + 1 Move mouse to (1, 2)
  • Ctrl + Alt + 2 Move mouse to (3, 4)
  • Ctrl + Alt + 3 Move mouse to (5, 6)
  • Ctrl + Alt + 4 Move mouse to (0, 0)

All the hotkeys are assigned in the format Ctrl + Alt + code, where code is the next available shortcut key from this list:
(caps) 1
(caps) 2
(caps) 3
(caps) 4
(caps) 5
(caps) 6
(caps) 7
(caps) 8
(caps) 9
(caps) 0
(num) 1
(num) 2
(num) 3
(num) 4
(num) 5
(num) 6
(num) 7
(num) 8
(num) 9
(num) 0

The source code
Here is the source code for JMouse:
// Copyright (C) 2011 By RhetoricalRuvim 
// Program:  JMouse 

#include <windows.h> 
#include <stdio.h> 

#define c_exit   'E' 

HINSTANCE hInstance; 
int use_verbose= 1; 

struct POINT_CHAIN { 
	int id; 
	POINT value; 
	struct POINT_CHAIN * first; 
	struct POINT_CHAIN * prev; 
	struct POINT_CHAIN * next; 

void * GatherCoordinates (char * cmd_line); 
void DestroyPointChain (struct POINT_CHAIN * pFirst); 

int WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCMD){ 
	int exit_id= 50; 
	struct POINT_CHAIN * pFirst; 
	struct POINT_CHAIN * pCurrent; 
	int i; 
	int j; 
	int count; 
	MSG msg; 
	LRESULT result; 
	if (use_verbose) printf ("JMouse - Win32 C Jump Mouse Program\r\nVersion:  1.0; original release. \r\n"); 
	if (use_verbose) printf ("Copyright (C) 2011 By RhetoricalRuvim\r\n\r\n"); 
	hInstance= hInst; 
	pFirst= GatherCoordinates ((char *)(GetCommandLine ())); 
	RegisterHotKey (0, exit_id, MOD_CONTROL | MOD_ALT, (UINT)(c_exit)); 
	i= 1; 
	j= 49; 
	pCurrent= pFirst; 
	// Register all the hotkeys. 
	// Order:  First, (caps) 1 - 9 and 0; then, (caps) A - Z; then, (num) 1 - 9 and 0. 
	while (pCurrent){ 
		if (j > 57) j= 48; 
		if (j < 48) j= 65; 
		if (j > 90) j= 97; 
		if (j > 105) j= 96; 
		if (!RegisterHotKey (0, i, MOD_CONTROL | MOD_ALT, (UINT)(j))) i--; 
		else pCurrent->id= i; 
		if (j == 48) j= 0; 
		if (j == 96) break;              // The limit of the hotkeys is 46 or less, depending on the successfullness of any of the hotkey registrations. 
		if (pCurrent->id) pCurrent= pCurrent->next; 
	count= i; 
	while (GetMessage (&msg, 0, 0, 0)){ 
		if (msg.message == WM_HOTKEY){ 
			if (msg.wParam == exit_id) break; 
			pCurrent= pFirst; 
			while (pCurrent){ 
				if (msg.wParam == pCurrent->id){ 
					SetCursorPos (pCurrent->value.x, pCurrent->value.y); 
					if (use_verbose) printf ("WM_HOTKEY (wParam = %d); cursor position= (%d, %d);\r\n", msg.wParam, pCurrent->value.x, pCurrent->value.y); 
					if (use_verbose) for (pCurrent= pFirst; pCurrent; pCurrent= pCurrent->next) 
						printf ("Tree; node id = %d; coordinates = (%d, %d);\r\n", pCurrent->id, pCurrent->value.x, pCurrent->value.y); 
				pCurrent= pCurrent->next; 
			result= 0; 
		} else if (msg.message == WM_CLOSE){ 
			// This is if we need to exit, for example if some process used the TASKKILL command on us. 
			ReplyMessage (0); 
		} else { 
			// Just ignore anything else. 
			result= 0; 
		ReplyMessage (result); 
	// Un-register all the hotkeys. 
	for (i= 0; i < count; i++) UnregisterHotKey (0, i); 
	UnregisterHotKey (0, exit_id); 
	// Free the memory from the point chain pieces. 
	DestroyPointChain (pFirst); 
	// (Exit.) 
	ExitProcess (0); 

void * GatherCoordinates (char * cmd_line){ 
	char exec [512]; 
	char string1 [1024]; 
	int x= 0; 
	int y= 0; 
	int number_processed; 
	struct POINT_CHAIN * pFirst; 
	struct POINT_CHAIN * pPrev; 
	struct POINT_CHAIN * pNext; 
	struct POINT_CHAIN * pNow; 
	pFirst= GlobalAlloc (GMEM_ZEROINIT, sizeof (struct POINT_CHAIN)); 
	// string1= cmd_line 
	sscanf (cmd_line, "%[^&]", string1); 
	// Skip any spaces. 
	sscanf (string1, "%*[ ]%[^&]", string1); 
	// Check if exec name quoted. 
	if (string1[0] == 34) sscanf (string1, "\"%[^\"]\" %[^&]", exec, string1);     // If so, skip quoted text. 
	else sscanf (string1, "%[^ ] %[^&]", exec, string1);                                     // Else, skip to next space. 
	// Check if blank. 
	if (!string1[0]) return pFirst; 
	else pNow= pFirst; 
	// Scan coordinates. 
	do { 
		number_processed= sscanf (string1, "( %d , %d ) %[^&]", &x, &y, string1); 
		if (use_verbose) printf ("Processing coordinate (%d, %d)... \r\n", x, y); 
		pNow->value.x= x; 
		pNow->value.y= y; 
		pNow->first= pFirst; 
		pNow->prev= pPrev; 
		pNow->next= pNext= GlobalAlloc (GMEM_ZEROINIT, sizeof (struct POINT_CHAIN)); 
		pPrev= pNow; 
		pNow= pNext; 
		if (number_processed < 3) break; 
	} while (string1[0]); 
	return pFirst; 
void DestroyPointChain (struct POINT_CHAIN * pFirst){ 
	struct POINT_CHAIN * pNow= pFirst; 
	struct POINT_CHAIN * pPrev; 
	while (pNow->next) pNow= pNow->next; 
	do { 
		pPrev= pNow->prev; 
		GlobalFree (pNow); 
		pNow= pPrev; 
	} while (pNow); 

  • 0

Also tagged with one or more of these keywords: linked list

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download