This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Why HIDEmuKbd project send char 'A' continuously?

In Hidemukbd.c,  I make some changes in function hidEmuKbd_HandleKeys as follows:

static void hidEmuKbd_HandleKeys( uint8 shift, uint8 keys )
{
  static uint8 prevKey1 = 0;
  static uint8 prevKey2 = 0;

  (void)shift;  // Intentionally unreferenced parameter

  if ( (keys & HAL_KEY_SW_1) && (prevKey1 == 0) )
  {
    // pressed  
   //Why always send char 'A' even if I tap the HAL_KEY_SW_1 only once ?
    hidEmuKbdSendReport( HID_KEYBOARD_A );//HID_KEYBOARD_A
    prevKey1 = 1;
  }
 /* I comment it intentionally to see what will happen  
else if (!(keys & HAL_KEY_SW_1) && (prevKey1 == 1) )
  {
    // released
    hidEmuKbdSendReport( KEY_NONE );
    prevKey1 = 0;
  }*/
  
  //for test key2
  if ( (keys & HAL_KEY_SW_2) && (prevKey2 == 0) )
  {
    // pressed
    hidEmuKbdSendReport( KEY_RIGHT_ARROW );//HID_KEYBOARD_A
    prevKey2 = 1;
  }
  else if ( !(keys & HAL_KEY_SW_2) && (prevKey2 == 1) )
  {
    // released
    hidEmuKbdSendReport( KEY_NONE );
    prevKey2 = 0;
  }
/* 
  if ( (keys & HAL_KEY_SW_2) && (prevKey2 == 0) )
  {
    // pressed 
    if ( !hidBootMouseEnabled )
    {
      hidEmuKbdSendReport( KEY_RIGHT_ARROW );//HID_KEYBOARD_M
    }
    else
    {
      hidEmuKbdSendMouseReport( MOUSE_BUTTON_1 );
    }
    prevKey2 = 1;
  }
  else if ( !(keys & HAL_KEY_SW_2) && (prevKey2 == 1) )
  {
    // releasedaaaaaaaaaaaaaaaaaaaaa
    if ( !hidBootMouseEnabled )
    {
      hidEmuKbdSendReport( KEY_NONE );
    }
    else
    {
      hidEmuKbdSendMouseReport ( MOUSE_BUTTON_NONE );
    }
    prevKey2 = 0;
  }
  */
}

After the project is built and downloaded, I tap the key for 'HAL_KEY_SW_2'  in a text file. My PC  print char  'A'  continuously. In my point of view, I think this struct :

if ( (keys & HAL_KEY_SW_1) && (prevKey1 == 0) )
  {
    // pressed
    hidEmuKbdSendReport( HID_KEYBOARD_A );//HID_KEYBOARD_A
    prevKey1 = 1;
  }

execute a time whenever I tape the  key.  Then I cannot understand why the program will print  continuous char  'A'?

  • Hello Uda,
    Try to include this again as i suspect you will need to send a report with the key NONE or else it will register as a long press that is never released:

    else if (!(keys & HAL_KEY_SW_1) && (prevKey1 == 1) )
    {
    // released
    hidEmuKbdSendReport( KEY_NONE );
    prevKey1 = 0;
    }
  • Hi, 
    Can I understand the repeated char 'A' is send by this functioin:
    hidEmuKbdSendReport( HID_KEYBOARD_A );//HID_KEYBOARD_A
    other than by this function:
    hidEmuKbd_HandleKeys( uint8 shift, uint8 keys ).
    a),If it is send by the former function, then I suppose that it register as a long press that is never released.
    b),If it is send by the latter one, then I suppose that the KEY_CHANGE event is triggered constantly.
    This is ture for the case of HAL_KEY_SW_2. When I press on this key, it can work the same behavior as the key HAL_KEY_SW_1. Can I think this is caused by the case b). Now I will check out why it will register as a long press that is never released in case a). Hope you can give me some suggestion.
    Best wishes from Uda.

  • Uda,
    Read my post carefully. Have you tried it yet?

    I think the idea here is to send two reports. One when you press and one when you release the key. ISR will trigger on rising or falling edge (depending on your configuration) when you push the button then a osal timer is initiated periodically to poll on the line to sense when you release the button. HalKeyPoll() will run periodically and if you release the button:

    /* Invoke Callback if new keys were depressed */
    if (notify && (pHalKeyProcessFunction))
    {
    (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);

    }

    pHalKeyProcessFunction=OnBoard_KeyCallback() -> OnBoard_SendKeys() (send KEY_CHANGE OSAL message) ........
    ....hidEmuKbd_ProcessOSALMsg handle message and call hidEmuKbd_HandleKeys()

    You must make sure to call hidEmuKbdSendReport( KEY_NONE ); in hidEmuKbd_HandleKeys.
  • Hi, Eirik V,
    I have try your suggestion, and it works fine. Thank you.