Can someone give me an example of how to use the ToAscii functions to convers windows virtual key codes to ascii characters? I've tried to search on google, but I can't find an example..

Recommended Answers

All 4 Replies

Here is an example, I think its in vb but you should be able to translate it easily to C.

Okay, I converted it to C but when I run it and press shift, it doesn't give me any characters, even after i lift the shift key up..

char MapKey(int key)
{
	char *result;
	int c;
	byte ks[256];

	GetKeyboardState(ks);
	result = malloc(3);
	result[0] = 0;

	c = ToAscii(key, MapVirtualKey(key,0), ks, &result[0], 0);

	if(c == 1)
	{
		c = result[0];
		free(result);
		return c;
	}

	free(result);
	return 0;
}

The shift key shows no output, by itself, so it has no ASCII code value. If the shift key is depressed, the OTHER keys will have a different ASCII value, but the shift key itself, doesn't need one.

There is a "shift in" and "shift out" at ASCII 14 and 15, but that is legacy stuff from telegraph days, iirc.

why is that program using malloc() to allocate just 3 bytes??? It seems such a waste of CPU cycles. Just declare it like this: char result[3]; then remove the malloc() and free()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.