Thursday, February 21, 2008

Capturing key inputs

Following on what I said yesterday, no emulator would be good enough without the ability to capture key inputs. This is pretty easy and can be quite useful in alot of situations. An exemple of this can be found in the form tutorial_Tetris. (Yes, there is a Tetris game in DAX for those who didn't know!)

The first thing you need to do is add a Tree control to your design and set it's "AutoDeclaration" to Yes. The Tree control allows us to capture the "keyDown" events by overwriting the method with the same name. We can then use the vKey that is passed to this method and do whatever we want when a key is pressed.

boolean keyDown(int vKey, boolean ctrl, boolean shift)
{
        boolean ret;

        ret = super(vKey, ctrl, shift);

        switch (vkey)
        {
                case 49: //1 (1)
                        keyPressed = 2;
                        break;
                case 81: //4 (Q)
                        keyPressed = 5;
                        break;
                case 50: //5 (W)
                        keyPressed = 6;
                        break;
        }
        return ret;
}

So for our emulator, we want to have the emulator focused on the Tree whenever it's not processing information. So after we execute an instruction, we give back the focus to the Tree like so :

void runEmu()
{
        int opcode;
        ;

        opcode = memory[pointer]
        this.execute(opcode);
        pointer++;

        if (emuRunning)
                this.setTimeOut(identifierStr(runEmu),0,false);
        Tree.setFocus();
}

This is just a rough example, but we could call methods from here to execute a certain peice of code or change the emu status (like to pause or play the emulator).

I almost posted today about the importance of making backups of your code but was too annoyed with the fact that I lost a day and a half worth of work :p.... So remember guys, ALWAYS backup your projects, even if automatic backups are suppose to be running every night.

No comments: