Author Topic: Working with keys  (Read 8461 times)

Offline evgenik

  • Newbie
  • *
  • Posts: 18
    • View Profile
Working with keys
« on: March 11, 2008, 05:10:49 PM »
Hi.
I want use with 4 keys in uTasker with M52235 micro (defined as IRQ1 ... IRQ4), total i have 5 combination when keys pressed: 1,2,3,4 - only one key pressed and 5th combination - two keys pressed. In file KeyScan.c has fnCheckKeyChanges() function where i can check changes into 2 arrays:  ucColsLast and ucCols (i have always both arrays = 0) and into application.c - after #ifdef SUPPORT_KEY_SCAN when I check pressed button by case IRQX_EVENT (where X - number of pressed key from 1 to 4). How can I determinate when user pressed 2 keys?
I am beginner with uTasker and microcontrollers.Thanks.
Evgeni Kosakovski.

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Working with keys
« Reply #1 on: March 11, 2008, 09:16:22 PM »
Hi Evgeni

The key pad support is an example of how a matrix keyboard can be controlled. If you configure
#define KEY_ROWS                  1
#define KEY_COLUMNS             4
it will more or less do a single 4 inputs, although a rather over-complicated method.
By simply reading the port and checking the 4 inputs is of course a much simpler method, but I don't think that this is the main topic here.

As you already know, the example generates events (on press and on release of keys) which are sent to the application task where they can write a message to the UART (or of course be used to do other jobs).

Since key presses and also key releases are events, it is in fact very easy for the application to also decide when certain combinations of keys are pressed, it must simply remember and react accordingly.

Here is an example (assuming the events KEY_EVENT_COL_1_ROW_1_PRESSED, KEY_EVENT_COL_1_ROW_1_RELEASED,
KEY_EVENT_COL_2_ROW_1_PRESSED, KEY_EVENT_COL_2_ROW_1_RELEASED, etc.)

static int iKeysPressed[4] = {0};

handle events
switch (event) {
    case KEY_EVENT_COL_1_ROW_1_PRESSED:
        iKeysPressed[0] = 1;
        break;
    case KEY_EVENT_COL_2_ROW_1_PRESSED:
        iKeysPressed[1] = 1;
        break;
    case KEY_EVENT_COL_3_ROW_1_PRESSED:
        iKeysPressed[2] = 1;
        break;
    case KEY_EVENT_COL_4_ROW_1_PRESSED:
        iKeysPressed[3] = 1;
        if (iKeysPressed[1] != 0) {  // special case when 4 is pressed while 2 is hold
            // do something here
        }

        break;
    case KEY_EVENT_COL_1_ROW_1_RELEASED:
        iKeysPressed[0] = 0;
        break;
    case KEY_EVENT_COL_2_ROW_1_RELEASED:
        iKeysPressed[1] = 0;
        break;
    case KEY_EVENT_COL_3_ROW_1_RELEASED:
        iKeysPressed[2] = 0;
        break;
    case KEY_EVENT_COL_4_ROW_1_RELEASED:
        iKeysPressed[3] = 0;
        break;
}


Regards

Mark

Offline evgenik

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Working with keys
« Reply #2 on: March 17, 2008, 10:18:32 AM »
Thanks.