title: Creating Alt Codes on a Laptop Without Them
body: <p>My laptop has a number pad, but it does not have a NumLock key, and the numpad is actually just a copy of the row of numbers above the letters. This is confirmed by the virtual key codes sent when I press these keys.</p>
  
  <p>I'm trying to develop a small program to mimic alt codes when the alt key and regular numbers are pressed. I use a low-level keyboard hook (I have a similar format working in another program), and first check to see if either alt key is down. If either is, I loop through the VK codes 0x30-0x39 (0-9 keys). If one of those is pressed down at that moment, I discard the actual keystroke by returning a value of 1, and instead send a numpad version of that key instead (alt is still pressed down at this time).</p>
  
  <p>I can confirm that the hook is being reached, and that the alt key being down is being recognized successfully. However, when I check for matches on 0-9, either only a couple are printed before nothing matches after that, or I have to lift up and press down the alt key every time I press a number. Additionally, one number may be printed 16 times after releasing alt having pressed a number, and then holding down alt and pressing another (this one is 16x).</p>
  
  <p>Also, I can confirm the SendInput sequence works via copying the part from the hook, putting it into the main function and replacing i with 0x30. Upon running, a 0 will be typed onto the text document.</p>
  
  <p>When holding down ALT(down)+6+5+ALT(up) in a text document, "65" is what shows. If I add another ALT(down)+6, 16 sixes appear.</p>
  
  <p>Hook Procedure:</p>
  
  <pre><code>LRESULT CALLBACK proc (int code, WPARAM wParam, LPARAM event) //hook proc
  {
      if (code &lt; HC_ACTION) //don't process if not meant to
          return CallNextHookEx (0, code, wParam, event); 
  
      if (GetAsyncKeyState (VK_MENU) &amp; 0x8000) //if either alt is down
      {
          for (int i = 0x30; i &lt;= 0x39; ++i) //loop 0-9
          {
              if (GetAsyncKeyState (i) &amp; 0x8000) //if index is down
              {
                  cout &lt;&lt; "MATCH\n"; //debug
  
                  input.ki.wVk = i + 0x30; //set VK code to numpad version of index
                  input.ki.dwFlags = 0; //key is being pressed
                  SendInput (1, &amp;input, sizeof (INPUT)); //send keystroke down
  
                  input.ki.dwFlags = KEYEVENTF_KEYUP; //key is being released
                  SendInput (1, &amp;input, sizeof (INPUT)); //send keystroke up
  
                  while (GetAsyncKeyState (i) &amp; 0x8000) //wait for normal key to be released
                      Sleep (10); //don't hog CPU
  
                  return 1; //discard normal key
              } //end if match
          } //end for
      } //end if alt
  
      return CallNextHookEx (0, code, wParam, event); //if any key not handled, pass on
  } //end function
  </code></pre>