-3

I want to achieve the following:

I press and release the C key, and the letter C is typed, as normal.

But instead, I want to press and release the C key, and have CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC.. typed continuously until I press C again.

How would the latter look in Delphi code as a key function?

Essentially, I have a hotkey assigned to a function, and it works, but I want to change it so that a single press of the hotkey causes the function to continue repeating until the hotkey is pressed again.

Here's the code:

lka_Skip:
if Game.Playing then
  if func.Modifier < 0 then
  begin
    if GameParams.NoAutoReplayMode then Game.CancelReplayAfterSkip := true;
    if CurrentIteration > (func.Modifier * -1) then
      GotoSaveState(CurrentIteration + func.Modifier)
    else
      GotoSaveState(0);
  end else if func.Modifier > 1 then
  begin
    fHyperSpeedTarget := CurrentIteration + func.Modifier;
  end else
    if fGameSpeed = gspPause then fForceUpdateOneFrame := true;

I can already add a "Hold" checkbox to the lka_Skip function, but I need a line of code which says "if the hold box is checked, perform the action repeatedly until the key is pressed again".

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Willicious
  • 13
  • 2

2 Answers2

0

If I understand your question correctly you are looking on how to toggle the boolean value between True and False state.

You can do this easily by using Boolean operator not. You can read more about boolean operators in Documentation

So the code for toggling some boolean value between True and False would be as simple as this:

SomeBooleanValue := not SomeBooleanValue;
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
0

In form's OnKeyPress, OnKeyDown or OnKeyUp, you check if the key pressed is 'C'. If it is, you start generating the 'ccccc' sequence, by starting a TTimer. You also set a boolean variable (let's call it GenerateC) to True.
enter image description here

Next time you press 'c' and enter the OnKeyPress event handler, you check if the variable is True or False. If True, you stop generating the 'ccccc' sequence (you stop the timer). If False, you start generating. Of course, you also negate the variable:

GenerateC:= not GenerateC;

Note: Your form might need to have the KeyPreview property set to True. KeyPreview is false by default!

enter image description here

If KeyPreview is true, keyboard events occur on the form before they occur on the active control. (The active control is specified by the ActiveControl property.)

If KeyPreview is false, keyboard events occur only on the active control.

Navigation keys (Tab, BackTab, the arrow keys, and so on) are unaffected by KeyPreview because they do not generate keyboard events. Similarly, when a button has focus or when its Default property is true, the Enter key is unaffected by KeyPreview because it does not generate a keyboard events.

I personally use KeyPreview a lot so I can close the form with Enter and Escape:

procedure TfrmCalendarEditor.FormKeyPress(Sender: TObject; var Key: Char);
begin
 if Ord(Key) = VK_RETURN then btnOkClick(Sender);
 if Ord(Key) = VK_ESCAPE then Close;
end;
Gabriel
  • 20,797
  • 27
  • 159
  • 293