From Clomosy Docs

(Created page with "The clSenderKey feature captures the codes of keys on the keyboard that have special functions, excluding standard character keys (a-z, 0-9, etc.), and performs specific actions based on them. These types of keys are generally referred to as "control keys" or "special keys." These control keys on the keyboard include arrow keys, function keys (F1-F12), Ctrl, Alt, Shift, Escape, Enter, Backspace, and other similar keys. To detect these keys, the tbeOnKeyDown or tbeOnKey...")
 
No edit summary
Line 82: Line 82:


'''Example 1: '''<br>
'''Example 1: '''<br>
In the following example, a form is created with an image that can be moved around the screen using the arrow keys. The SetMotion procedure checks the key code captured by clSenderKey. When the left arrow key (37) is pressed, the image rotates 180 degrees and moves left by 5 units unless it has reached the left edge of the form. When the right arrow key (39) is pressed, the image rotates back to 0 degrees and moves right by 5 units unless it has reached the right edge. When the up arrow key (38) is pressed, the image rotates 270 degrees and moves up by 5 units unless it has reached the top edge. When the down arrow key (40) is pressed, the image rotates 90 degrees and moves down by 5 units unless it has reached the bottom edge. If any other keys are pressed, a message prompts the user to use only the arrow keys. The form is created, an image is added and centered, and an event handler is set up to call the SetMotion procedure on key down.


:'''TRObject Syntax'''
:'''TRObject Syntax'''
<pre>
var
  Form1:TCLForm;
  Image1 : TclImage;
 
procedure SetMotion;
begin
  if Form1.clSenderKey = 37 then //LEFT ok tuşu 37
  begin
    Image1.RotationAngle := 180;
    if Image1.Position.X <= 0 then
      ShowMessage('Can''t Go Any Further Left');
    else
      Image1.Position.X := Image1.Position.X - 5;
  end;
  else if Form1.clSenderKey = 39 then //RIGHT ok tuşu 39
  begin
    Image1.RotationAngle := 0;
    if Image1.Position.X >= Form1.clWidth then
      ShowMessage('Can''t Go Any Further Right');
    else
      Image1.Position.X := Image1.Position.X + 5;
  end;
  else if Form1.clSenderKey = 38 then //yukarı ok tuşu 38
  begin
    Image1.RotationAngle := 270;
    if Image1.Position.Y <= 0 then
      ShowMessage('Can''t Go Any Further Higher');
    else
      Image1.Position.Y := Image1.Position.Y - 5;
  end
  else if Form1.clSenderKey = 40 then //aşağı ok tuşu 40
  begin
    Image1.RotationAngle := 90;
    if Image1.Position.Y >= Form1.clHeight then
      ShowMessage('Can''t Go Any Further Lower');
    else
      Image1.Position.Y := Image1.Position.Y + 5;
  end else
  begin
    ShowMessage('Please use only directional arrow keys..');
  end;
end;


begin
  Form1 := TCLForm.Create(Self);
  Image1 := Form1.AddNewImage(Form1,'Image1');
  Image1.Align := AlNone;
  Image1.Position.X := Form1.clWidth / 2;
  Image1.Position.Y := Form1.clHeight / 2;
  Image1.Width := 100;
  Image1.Height := 100;
  Form1.setImage(Image1,'https://clomosy.com/demos/circled-right.png');
 
  Form1.AddNewEvent(Form1,tbeOnKeyDown,'SetMotion');
  Form1.Run;
end;
</pre>
:'''Base Syntax'''
:'''Base Syntax'''
<pre>
//Clomosy.RunUnit('clSenderKey');
var
  Form1:TCLForm;
  Image1 : TclImage;
 
void SetMotion;
{
  if(Form1.clSenderKey == 37) //LEFT ok tuşu 37
  {
    Image1.RotationAngle = 180;
    if(Image1.Position.X <= 0)
      ShowMessage('Can''t Go Any Further Left');
    else
      Image1.Position.X = Image1.Position.X - 5;
  }
  else if(Form1.clSenderKey == 39) //RIGHT ok tuşu 39
  {
    Image1.RotationAngle = 0;
    if(Image1.Position.X >= Form1.clWidth)
      ShowMessage('Can''t Go Any Further Right');
    else
      Image1.Position.X = Image1.Position.X + 5;
  }
  else if(Form1.clSenderKey == 38) //yukarı ok tuşu 38
  {
    Image1.RotationAngle = 270;
    if(Image1.Position.Y <= 0)
      ShowMessage('Can''t Go Any Further Higher');
    else
      Image1.Position.Y = Image1.Position.Y - 5;
  }
  else if(Form1.clSenderKey == 40) //aşağı ok tuşu 40
  {
    Image1.RotationAngle = 90;
    if(Image1.Position.Y >= Form1.clHeight)
      ShowMessage('Can''t Go Any Further Lower');
    else
      Image1.Position.Y = Image1.Position.Y + 5;
  }
  else
  {
    ShowMessage('Please use only directional arrow keys..');
  }
}
{
  Form1 = TCLForm.Create(Self);
  Image1 = Form1.AddNewImage(Form1,'Image1');
  Image1.Align = AlNone;
  Image1.Position.X = Form1.clWidth / 2;
  Image1.Position.Y = Form1.clHeight / 2;
  Image1.Width = 100;
  Image1.Height = 100;
  Form1.setImage(Image1,'https://clomosy.com/demos/circled-right.png');
 
  Form1.AddNewEvent(Form1,tbeOnKeyDown,'SetMotion');
  Form1.Run;
}
</pre>

Revision as of 13:42, 7 August 2024

The clSenderKey feature captures the codes of keys on the keyboard that have special functions, excluding standard character keys (a-z, 0-9, etc.), and performs specific actions based on them.

These types of keys are generally referred to as "control keys" or "special keys." These control keys on the keyboard include arrow keys, function keys (F1-F12), Ctrl, Alt, Shift, Escape, Enter, Backspace, and other similar keys.

To detect these keys, the tbeOnKeyDown or tbeOnKeyUp events are used, as these events use virtual key codes and can capture keys that do not represent characters. For example, they can be used to move an image (TclImage) on the screen using the arrow keys.

Below is a table of control keys.

Key Code Key
8 Backspace
9 Tab
13 Enter
16 Shift
17 Ctrl
18 Alt
19 Pause
20 CapsLock
27 Escape
33 PageUp
34 PageDown
35 End
36 Home
37 ArrowLeft
38 ArrowUp
39 ArrowRight
40 ArrowDown
45 Insert
46 Delete
91 Windows Key
93 ContextMenu/ Windows Menu
112 F1
113 F2
114 F3
115 F4
116 F5
117 F6
118 F7
119 F8
120 F9
121 F10
122 F11
123 F12
144 NumLock
145 ScrollLock

Example 1:
In the following example, a form is created with an image that can be moved around the screen using the arrow keys. The SetMotion procedure checks the key code captured by clSenderKey. When the left arrow key (37) is pressed, the image rotates 180 degrees and moves left by 5 units unless it has reached the left edge of the form. When the right arrow key (39) is pressed, the image rotates back to 0 degrees and moves right by 5 units unless it has reached the right edge. When the up arrow key (38) is pressed, the image rotates 270 degrees and moves up by 5 units unless it has reached the top edge. When the down arrow key (40) is pressed, the image rotates 90 degrees and moves down by 5 units unless it has reached the bottom edge. If any other keys are pressed, a message prompts the user to use only the arrow keys. The form is created, an image is added and centered, and an event handler is set up to call the SetMotion procedure on key down.

TRObject Syntax
var
  Form1:TCLForm;
  Image1 : TclImage;
  
procedure SetMotion;
begin
  if Form1.clSenderKey = 37 then //LEFT ok tuşu 37
  begin
    Image1.RotationAngle := 180;
    if Image1.Position.X <= 0 then
      ShowMessage('Can''t Go Any Further Left');
    else 
      Image1.Position.X := Image1.Position.X - 5;
  end;
  else if Form1.clSenderKey = 39 then //RIGHT ok tuşu 39
  begin
    Image1.RotationAngle := 0;
    if Image1.Position.X >= Form1.clWidth then
      ShowMessage('Can''t Go Any Further Right');
    else 
      Image1.Position.X := Image1.Position.X + 5;
  end;
  else if Form1.clSenderKey = 38 then //yukarı ok tuşu 38
  begin
    Image1.RotationAngle := 270;
    if Image1.Position.Y <= 0 then
      ShowMessage('Can''t Go Any Further Higher');
    else 
      Image1.Position.Y := Image1.Position.Y - 5;
  end
  else if Form1.clSenderKey = 40 then //aşağı ok tuşu 40
  begin
    Image1.RotationAngle := 90;
    if Image1.Position.Y >= Form1.clHeight then
      ShowMessage('Can''t Go Any Further Lower');
    else 
      Image1.Position.Y := Image1.Position.Y + 5;
  end else
  begin
    ShowMessage('Please use only directional arrow keys..');
  end;
end;

begin
  Form1 := TCLForm.Create(Self);
  Image1 := Form1.AddNewImage(Form1,'Image1');
  Image1.Align := AlNone;
  Image1.Position.X := Form1.clWidth / 2;
  Image1.Position.Y := Form1.clHeight / 2;
  Image1.Width := 100;
  Image1.Height := 100;
  Form1.setImage(Image1,'https://clomosy.com/demos/circled-right.png');
  
  Form1.AddNewEvent(Form1,tbeOnKeyDown,'SetMotion');
  Form1.Run;
end;
Base Syntax
//Clomosy.RunUnit('clSenderKey');

var
  Form1:TCLForm;
  Image1 : TclImage;
  
void SetMotion;
{
  if(Form1.clSenderKey == 37) //LEFT ok tuşu 37
  {
    Image1.RotationAngle = 180;
    if(Image1.Position.X <= 0)
      ShowMessage('Can''t Go Any Further Left');
    else 
      Image1.Position.X = Image1.Position.X - 5;
  }
  else if(Form1.clSenderKey == 39) //RIGHT ok tuşu 39
  {
    Image1.RotationAngle = 0;
    if(Image1.Position.X >= Form1.clWidth)
      ShowMessage('Can''t Go Any Further Right');
    else 
      Image1.Position.X = Image1.Position.X + 5;
  }
  else if(Form1.clSenderKey == 38) //yukarı ok tuşu 38
  {
    Image1.RotationAngle = 270;
    if(Image1.Position.Y <= 0)
      ShowMessage('Can''t Go Any Further Higher');
    else 
      Image1.Position.Y = Image1.Position.Y - 5;
  }
  else if(Form1.clSenderKey == 40) //aşağı ok tuşu 40
  {
    Image1.RotationAngle = 90;
    if(Image1.Position.Y >= Form1.clHeight)
      ShowMessage('Can''t Go Any Further Lower');
    else 
      Image1.Position.Y = Image1.Position.Y + 5;
  }
  else
  {
    ShowMessage('Please use only directional arrow keys..');
  }
}

{
  Form1 = TCLForm.Create(Self);
  Image1 = Form1.AddNewImage(Form1,'Image1');
  Image1.Align = AlNone;
  Image1.Position.X = Form1.clWidth / 2;
  Image1.Position.Y = Form1.clHeight / 2;
  Image1.Width = 100;
  Image1.Height = 100;
  Form1.setImage(Image1,'https://clomosy.com/demos/circled-right.png');
  
  Form1.AddNewEvent(Form1,tbeOnKeyDown,'SetMotion');
  Form1.Run;
}