c++c++builder-10.2-tokyo

Beeping while KeyDown is triggered


I have a TListView (it is focused all the time) and an OnKeyDown event handler on my Form (its KeyPreview property is true).

playlist is my TListView component (Style = vsReport).

void __fastcall Tmform::mformKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
  if(Shift.Contains(ssCtrl))            // hotkeys with CTRL
  {
     switch(Key)
     {
        case vkF: findDoublesbtnClick(mform);  break;        // [FIND TWINS]
        case vkD: dbsClick(mform);             break;        // [DELETE BAD SONGS]
        case vkA: playlist->SelectAll();       break;        // [CTRL + A]
        case vkS: settingsClick(mform);        break;        // [SETTINGS]
     }
  }
  else                                  // just keys
  {
     switch(Key)
     {
        case vkReturn:  if(playlist->SelCount) pl.refreshSong();        break;   // [ENTER]
        case vkDelete:   af.deleteFiles();      break;        // [DELETE]
        case vkSpace:
        case vkNumpad3:  pl.playPauseResume();  break;
        case vkSubtract: prevbtnClick(mform);   break;        // [PREVIOUS]
        case vkAdd:      nextbtnClick(mform);   break;        // [NEXT]
        case vkC:        counterClick(mform);   break;        // [LISTENINGS WIN]
     }
}

Why does it beep when I press any key (with the TListView focused)?


Solution

  • So, I found out, why it beeps. It seems to be standard behavior of TListView component. When one item in TListView is selected (and TListView has focus), any character input triggers "select the typed item" method, that trying to find and select item by our input.

    That was the answer I was interested about. To make work hot keys (including one-key) I used next code:

    void __fastcall TForm1::ListViewKeyPress(TObject *Sender, System::WideChar &Key)
    {  Key = 0; }     // here TListView calls "find" method. I reset Key value,
                      // thus I have only vkCode for FormKeyDown which triggers
                      // after FormKeyPress
    void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
    {
        if (Shift.Contains(ssAlt) && Key == vkF)                // use any key or shortcut
        {  Form1->Color = RGB(random(255), 0, 0);  return; }    // you wish
        if (Key == vkF)
           Form1->Color = RGB(0, random(255), 0);
        if (Key == vkSpace)
           Form1->Color = RGB(0, 0, random(255));
    }
    

    It works with all existing PC keyboard layouts. But with 'alt' and 'win' keys still beeps, because any key with 'alt' or 'win' does not triger the ListViewKeyPress event.

    Thank for your help!