title: Chip8 Emulator - User Input
body: <p>I have been writing a chip8 Emulator -- <a href="http://en.wikipedia.org/wiki/CHIP-8" rel="nofollow">http://en.wikipedia.org/wiki/CHIP-8</a></p>
  
  <p>I have tested all of the opcodes as well as the graphics calculations and I am now struggling with the user input. I have the following method monitoring user input and altering the registers as needed (When using the chip8 the user input alters the corresponding memory register - E.G. hitting '0' sets the V0 register to 0.</p>
  
  <p>My problem is that I have the following code fetching and calculating each opcode and its operation contained in a while loop. And while this is running my application cannot detect user input. So the ROMS start and just stay locked in place waiting on a register change or user input. It keeps getting stuck in an infinite loop, I tried to implement a global Boolean RUN, and it is not detected after the while loop is initiated. I'm assuming it is out of the scope of the loop, but from what I have read, the keyboard event triggers an interrupt that should be visible from almost anywhere, any Thoughts?</p>
  
  <p>This is what calculates and parses the opcode   </p>
  
  <pre><code> private void button1_Click(object sender, EventArgs e)
      {
          // This will become the run function 
           do{
          for (int i = 0; i &lt; 2; i++)
          {
              opc[i] = mem[mc]; // fetching the instruction from the memory array
              mc++;
          }
          cibox.Clear(); // Just clearing Debugging text boxes in the UI
          pcbox.Clear();
          pc++;
          pcbox.Text += pc;
          cibox.Text += opc[0].ToString("X2") + "-" + opc[1].ToString("X2");
          calculations(opc); // Parses the Opcode and does the corresponding operation
           }while(run);
      }
  </code></pre>
  
  <p>And this method is controlling the user input...</p>
  
  <pre><code>  protected override void OnKeyDown(KeyEventArgs keyEvent) // Listens for Keyboard events! Read More: http://www.geekpedia.com/tutorial53_Getting-input-from-keyboard.html
      {
          keyPress = true;
          //Gets the key code found at keyEvent...
          MessageBox.Show("KeyCode: " + keyEvent.KeyCode.ToString());
          String register = keyEvent.KeyCode.ToString();
          if (register == "Escape")
          {
  
              Application.Exit();
              run = false;
          }
          try
          {
              registerVal = int.Parse(register, System.Globalization.NumberStyles.HexNumber); //  Second Nibble! --&gt; Int Format 
          }
          catch (System.ArgumentNullException e)
          {
              return;
          }
          catch (System.ArgumentException)
          {
              return;
          }
          catch (System.FormatException)
          {
              return;
          }
          catch (System.OverflowException)
          {
              return;
          }
          if (registerVal &gt;= 208)
          {
              registerVal = registerVal - 208;
          }
          if (registerVal &lt;= 15)
          {
              mem[registerVal] = (byte)registerVal;
          }
          display(); // Alters UI to display state of registers, etc
      }
  </code></pre>
  
  <p>So I have now tried the Game Loop Idea, but I cannot find a way to make a method in C# that will return a key press. Maybe I am missing something here, but I cannot seem to figure it out!</p>
  
  <p>I also tried another method involving running the CPU calculations in a separate thread, and this is causing a slight delay issue. </p>
  
  <p>I would really like to see an example of a method in C# that returns the value of a key being pressed that I can call within the While loop!</p>