Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On key up 1

Status
Not open for further replies.

Wings

Programmer
Feb 14, 2002
247
US
Hello,
I have the following code in my FormKeyUp handler

if (Key == 'k')
{
//Do stuff
{

For some reason when I press the + key, it enters this conditional. The plus key virtual key code is VK_ADD, why would it this it is the K key?

Thanks
 
This is because you are mixing Virtual Key Codes with ascii values. The ascii value of 'k' is 107 which is also the virtual key code VK_ADD.

If testing for alphabetic keys in your code, use the uppercase or the virtual key code (VK_K), not the lower case and then use TShiftState to determine if it is upper or lower case.

Code:
  if (Key == 'K' && !Shift.Contains(ssShift))
  {
    // do something
  }

The ascii key value only matches the virtual key code for uppercase alphabetic keys and the numeric keys (not the numeric keys on the keypad). Of course with the numeric keys, you would also have to check the shift state to determine if it is say the '8' or the '*'.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top