Jump to content

Recommended Posts

Posted

In VCL I block when the User presses Enter and does not release the key.
I use the Component

"ApplicationEvents"
using the event "OnMessage"

var
 Form1: TForm1;
 Count_Enter : Integer = 0;
 MostrouMsgTeclaPressionada : Boolean = False;

procedure TForm1.ApplicationEvents1Message(var Msg: TMsg; var Handled: Boolean);

 Procedure P_MensagemKeyPrecionada(Tecla: String);
 Begin
  MostrouMsgTeclaPressionada := True;
  Showmessage('Verifique a Tecla "' + Tecla + '" pois foi precionada muitas vezes e isso não é possivel.');
  MostrouMsgTeclaPressionada := False;
 End;

begin
 if (Msg.message = WM_KEYUP) then 
  Count_Enter := 0;

 if (Msg.message = WM_KEYDOWN) then begin
  if (Msg.wParam = VK_RETURN) Or (Msg.wParam = VK_TAB) then Begin
   
   Inc(Count_Enter);

   if Count_Enter > 1 Then
    if Not MostrouMsgTeclaPressionada Then
      P_MensagemKeyPrecionada('Enter');
      
  end;
 end;

End;

How could I do this in Unigui ?

Posted

Hello,

Do you want to detect Enter and Tab key presses globally (at the document level), or only within a specific component such as UniMemo?

Posted
1 hour ago, Sherzod said:

Hello,

Do you want to detect Enter and Tab key presses globally (at the document level), or only within a specific component such as UniMemo?

At a global level in any component, form, etc...

Posted
1 minute ago, Luciano França said:

At a global level in any component, form, etc...

Just out of curiosity — may I ask what the reason is for detecting Enter and Tab globally?

Posted
7 minutes ago, Sherzod said:

Just out of curiosity — may I ask what the reason is for detecting Enter and Tab globally?

 

My clients have the habit of pressing a key and holding it down, especially the enter key. Some of my modules show several confirmation messages, so holding it down will confirm all of them. Because of this, in VCL I block the stuck pressing of keys.

Posted
13 minutes ago, Luciano França said:

Because of this

Could you review this code and let us know if it suits your needs?

MainForm.Script ->

// Timestamps for the last Enter and Tab key press
let lastEnterTime = 0;
let enterCount = 0;

let lastTabTime = 0;
let tabCount = 0;

// Timers to reset counters after a delay
let timerEnter = null;
let timerTab = null;

// Global keydown event listener
document.addEventListener('keydown', function (e) {
  const now = Date.now();

  // === ENTER logic ===
  if (e.key === 'Enter') {
    // If key pressed again within 500ms, consider it repeated
    if (now - lastEnterTime < 500) {
      enterCount++;
    } else {
      enterCount = 1; // Reset count if delay is too long
    }
    lastEnterTime = now;

    // Show warning after 5 fast presses
    if (enterCount >= 5) {
      alert('You are pressing Enter too often. Please be careful.');
      enterCount = 0;
    }

    // Reset enter counter after 1.5 seconds of inactivity
    clearTimeout(timerEnter);
    timerEnter = setTimeout(() => { enterCount = 0; }, 1500);
  }

  // === TAB logic ===
  if (e.key === 'Tab') {
    if (now - lastTabTime < 500) {
      tabCount++;
    } else {
      tabCount = 1;
    }
    lastTabTime = now;

    if (tabCount >= 5) {
      alert('You are pressing Tab too often. Please stop.');
      tabCount = 0;
    }

    clearTimeout(timerTab);
    timerTab = setTimeout(() => { tabCount = 0; }, 1500);
  }
});

 

  • Thanks 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...