Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Simple keypress question

Hello everyone. I have a simple question that's been keeping me stumped lately:

How is it possible to invalidate a keyread while the previous keyevent is still in effect?

Example of problem: Someone presses 'a' on the keyboard 7 times. In effect, this would mean that the key-handling event will be carried out 7 times in a row. If these key-handling events take a while, the program will be continuing these events for a long period of time. What I would like to do is to invalidate all keyreads until a keyhandling event is finished.

Thanks in advance.

Comments

  • dj_sanniedj_sannie Posts: 92Member
    [b][red]This message was edited by dj_sannie at 2004-3-30 15:59:15[/red][/b][hr]
    : Hello everyone. I have a simple question that's been keeping me stumped lately:
    :
    : How is it possible to invalidate a keyread while the previous keyevent is still in effect?
    :
    : Example of problem: Someone presses 'a' on the keyboard 7 times. In effect, this would mean that the key-handling event will be carried out 7 times in a row. If these key-handling events take a while, the program will be continuing these events for a long period of time. What I would like to do is to invalidate all keyreads until a keyhandling event is finished.
    :
    : Thanks in advance.
    :
    Hi,

    U Could write a thread to handel the key actions.
    U can get something like this if u use a thread.
    [code]
    procedure TFormMain.FormKeyDown(Sender: TObject; var Key: Word;
    Shift: TShiftState);
    begin
    if( MyThread == nil )then MyThread := MyThread.Create();
    end;
    [/code]
    Only one if statement left. that won't hang the program :). It is not realy hard to make a thread, just look it up in the help files "TThread".

    U Could also play with the time. But remember that this will only work if u have one single OnKeyPress event in you're application.
    [code]
    var
    LastKeyPressExit: Cardinal;

    procedure TFormMain.FormKeyDown(Sender: TObject; var Key: Word;
    Shift: TShiftState);
    begin
    if( LastKeyPressExit+50 < GetTickCount )then
    brgin
    // Code
    LastKeyPressExit := GetTickCount;
    end;
    end;
    [/code]

    I hope this is helps u.

    Greets DJ Sannie.
  • Srki_KiSrki_Ki Posts: 26Member
    Try something like this. Create one thread object witch will perform than long time operations. Declare "flag" variable, some thing like IsThreadRunnig:Boolean. When you recive OnChar(or any other key event) you check the flag variable. If the thread is running you do nothing and if it doesn't set the flag variable and start the thread. If you use this method you can also cancel thread execution in response to some other event.
  • Srki_KiSrki_Ki Posts: 26Member
    Threads are the best if you dont want your application to stop responding but if you realy want just to remove some messages you can use some thing like this

    [code]
    procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
    Shift: TShiftState);
    var
    M:tagMSG;
    begin

    {Long time function goes here}

    while PeekMessage(M, Handle, WM_KEYDOWN, WM_KEYDOWN, 0) <> false do
    begin
    If M.wParam=Ord('B') then {Check to see if we realy want to remove this message}
    PeekMessage(M, Handle, WM_KEYDOWN, WM_KEYDOWN, 1);
    end;
    end;
    [/code]

    You check M.wParam for any character that you want to remove.
Sign In or Register to comment.