Jump to content

How to detect OnkeyDown events "UniSyntaxEdit"


Luciano França

Recommended Posts

I have tried the following approaches but none work

function beforeInit(sender, config)
{
   config.keyMap = {
        ENTER: {
            handler: function() {
              ajaxRequest(sender, '_ENTER', []);
            }
        },
        F2: {
            handler: function() {
              ajaxRequest(sender, '_F2', []);
            }
        },
        ESC: {
            handler: function() {
              ajaxRequest(sender, '_ESC', []);
            }
        },
        DELETE: {
            handler: function() {
              ajaxRequest(sender, '_DELETE', []);
            }
        }                
    }     
}


procedure TMainForm.UniSyntaxEdit1AjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings);
begin
  if EventName = '_ENTER' then
  begin
    ShowMessage('You have pressed the <b>ENTER</b> key.');
  end;
  if EventName = '_F2' then
  begin
    ShowMessage('You have pressed the <b>F2</b> key.');
  end;
  if EventName = '_ESC' then
  begin
    ShowMessage('You have pressed the <b>ESC</b> key.');
  end;
  if EventName = '_DELETE' then
  begin
    ShowMessage('You have pressed the <b>DELETE</b> key.');
  end;

end;

And

function keydown(sender, e, eOpts)
{
   if (e.getKey() == 13 ) {
    alert('Enter');
    ajaxRequest(sender, 'doEnter', []);
   }
}

procedure TMainForm.UniSyntaxEdit1AjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings);
begin
  if EventName = 'doEnter' then
  begin
    ShowMessage('You have pressed the <b>ENTER</b> key.');
  end;
End;

 

Link to comment
Share on other sites

On 7/6/2023 at 2:11 AM, Sherzod said:

Hello @Luciano França

Firstly, it is better to use UniSyntaxEditEx instead.

Secondly, I will try to give a solution based on which component you will use.

Using the "UniSyntaxEditEx" component I can capture the Keys but I cannot  

selectionStart  

And

selectionEnd

This approach doesn't work

procedure TMainForm.UniFormCreate(Sender: TObject);
begin
  with UniSyntaxEditEx1.JSInterface do Begin
    JSAddListener('blur', 'function(){ajaxRequest(this, "_blur", ["cpos="+this.inputEl.dom.selectionStart])}');
    JSAddListener('blur', 'function(){ajaxRequest(this, "_blur", ["lpos="+this.inputEl.dom.selectionEnd])}');  
  End;

end;

procedure TMainForm.UniSyntaxEditEx1AjaxEvent(Sender: TComponent; EventName: string;
  Params: TUniStrings);
begin
  if EventName = '_blur' then begin
    (Sender as TUniSyntaxEditEx).CustomAttribs.Values['cpos'] := Params.Values['cpos'];
    (Sender as TUniSyntaxEditEx).CustomAttribs.Values['lpos'] := Params.Values['lpos'];  
   End;

end;

 

Link to comment
Share on other sites

14 hours ago, Luciano França said:

I've already seen this topic and it doesn't help me.

Because what I need is to get the "get" positions

As I mentioned, I need to know the

selectionStart   and     selectionEnd

  

On 7/5/2023 at 9:08 AM, Sherzod said:

and analyze the solutions

 

Link to comment
Share on other sites

12 hours ago, Sherzod said:

  

 

 

This Post is mine and the codes posted don't work with "UniSyntaxEdit"

this code below does not work

procedure TMainForm.UniFormCreate(Sender: TObject);
begin
  with UniSyntaxEditEx1.JSInterface do
    JSAddListener('blur', 'function(){ajaxRequest(this, "_blur", ["cpos="+this.inputEl.dom.selectionStart])}');

end;

procedure TMainForm.UniSyntaxEditEx1AjaxEvent(Sender: TComponent; EventName: string;
  Params: TUniStrings);
begin
  if EventName = '_blur' then
    (Sender as TUniSyntaxEditEx).CustomAttribs.Values['cpos'] := Params.Values['cpos'];

end;

 

 

Link to comment
Share on other sites

From what I understand the "UniSyntaxEditEx" component doesn't have "SelectionStart" so how could I get the line and character where the cursor is?

How could I return using this information

codeEditor.editor.cursorPosition().character

codeEditor.editor.cursorPosition().line

Can you create an example for me because I didn't find anything on the forum that returns this information

Link to comment
Share on other sites

7 hours ago, Sherzod said:

And, remind me again, what's it all for !?

So I need a way to return these two pieces of information from "line" and "ch" could you post a "Get" capture code for me.

The reason is that in several situations in VCL I used "CaretPos.X" and "CaretPos.Y" to know where the mouse cursor is

With "UniMemo" I achieved this using "selectionStart" but with "UniSyntaxEditEx" there is no "selectionStart" so I need another way to achieve this.

Ps. I already tried to adapt this code that you posted above but I couldn't return these two information "line" and "ch"

function afterCreate(sender) 
{
    sender.insertTextAtCursor = function(str) {
        var editor = this.codeEditor;
        var selection = editor.getSelection();

        if (selection.length > 0) {
            editor.replaceSelection(str);
        } else {     
            var doc = editor.getDoc();
            var cursor = doc.getCursor();  
            var pos = {
                line: cursor.line,
                ch: cursor.ch
            };   
            doc.replaceRange(str, pos); 
        } 
    }
}

 

Link to comment
Share on other sites

14 minutes ago, Sherzod said:

What do you mean by saying this? 

Why do you need it, to insert text or what? If it's an insert then this works also for UniSyntaxEditEx.

If the Colleague can please assemble a Code for me that returns me to "Line" and "Ch"

As I already mentioned, I have several situations, which make it difficult to explain here all the places of my ERP that I need.

If you can put me a code I would be very grateful.

I tested something like this
no longer works

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
 with UniSyntaxEditEx1, UniSyntaxEditEx1.JSInterface do Begin
  JSCode('Ext.defer(function(){ajaxRequest(' + JSName + ', "getSelection", ["selectedText="+' + JSName + .codeEditor.getDoc().getCursor().line])}, 0);')
 end;
End;

procedure TMainForm.UniButton2Click(Sender: TObject);
begin
 with UniSyntaxEditEx1, UniSyntaxEditEx1.JSInterface do Begin
  JSCode('Ext.defer(function(){ajaxRequest(' + JSName + ', "getSelection", ["selectedText="+' + JSName + .codeEditor.getDoc().getCursor().ch])}, 0);')
 end;
End;


procedure TMainForm.UniSyntaxEditEx1AjaxEvent(Sender: TComponent; EventName: string; Params: TUniStrings);
begin
 if EventName = 'getSelection' then
  UniEdit1.Lines.Text := Params.Values['selectedText'];
end;

 

Link to comment
Share on other sites

I don't know what and how you are doing, but the solution above works for me.

procedure TMainForm.UniButton4Click(Sender: TObject);
begin
  with UniSyntaxEditEx1, UniSyntaxEditEx1.JSInterface do Begin
    JSCode('Ext.defer(function(){ajaxRequest(' + JSName + ', "getSelection", ["selectedText="+' + JSName + '.codeEditor.getDoc().getCursor().line])}, 0);')
  end;
end;

procedure TMainForm.UniSyntaxEditEx1AjaxEvent(Sender: TComponent;
  EventName: string; Params: TUniStrings);
begin
if EventName = 'getSelection' then
  UniEdit2.Text := Params.Values['selectedText'];
end;

image.png.0ca77543f817d069783d64fa8d1db745.png

Link to comment
Share on other sites

51 minutes ago, Sherzod said:

I don't know what and how you are doing, but the solution above works for me.

procedure TMainForm.UniButton4Click(Sender: TObject);
begin
  with UniSyntaxEditEx1, UniSyntaxEditEx1.JSInterface do Begin
    JSCode('Ext.defer(function(){ajaxRequest(' + JSName + ', "getSelection", ["selectedText="+' + JSName + '.codeEditor.getDoc().getCursor().line])}, 0);')
  end;
end;

procedure TMainForm.UniSyntaxEditEx1AjaxEvent(Sender: TComponent;
  EventName: string; Params: TUniStrings);
begin
if EventName = 'getSelection' then
  UniEdit2.Text := Params.Values['selectedText'];
end;

image.png.0ca77543f817d069783d64fa8d1db745.png

 

My problem is that I need to Get the "Line" and the "Ch" from 3 components
"TUniMemo",   "TUniSyntaxEditEx"   and     "TUniHTMLMemo""

This code I posted above I was testing a "UniHTMLMemo"  however this code only works with "UniSyntaxEditEx"

  That way I'm almost there:


   With "TUniMemo" I can with "SelectionStart"

 with UniMemo1.JSInterface do
    JSAddListener('blur', 'function(){ajaxRequest(this, "_blur", ["cpos="+this.inputEl.dom.selectionStart])}');


   With "TUniSyntaxEditEx" I can with

  with UniSyntaxEditEx1, UniSyntaxEditEx1.JSInterface do Begin
    JSCode('Ext.defer(function(){ajaxRequest(' + JSName + ', "getSelection", ["selectedText="+' + JSName + '.codeEditor.getDoc().getCursor().ch])}, 0);')
  end;

 

   Now how to do with "TUniHTMLMemo"  ?

 

 

Link to comment
Share on other sites

1 hour ago, Sherzod said:

Sorry, then I did not understand your case, and I think I can not help you with this question.

I will try to explain better

I already managed to get the "line" and "Ch" of two components  "TUniMemo" and  "TUniSyntaxEditEx"

because neither of these two approaches works with "TUniHTMLMemo" ?

 // Error 
 with UniHTMLMemo1.JSInterface do
  JSAddListener('blur', 'function(){ajaxRequest(this, "_blur", ["cpos="+this.inputEl.dom.selectionStart])}');


  // Error
  with UniHTMLMemo1, UniHTMLMemo1.JSInterface do Begin
    JSCode('Ext.defer(function(){ajaxRequest(' + JSName + ', "getSelection", ["selectedText="+' + JSName + '.codeEditor.getDoc().getCursor().ch])}, 0);')
  end;

  // Error
  with UniHTMLMemo1, UniHTMLMemo1.JSInterface do Begin
    JSCode('Ext.defer(function(){ajaxRequest(' + JSName + ', "getSelection", ["selectedText="+' + JSName + '.htmlEditor.getDoc().getCursor().ch])}, 0);')
  end;

  // Error
  with UniHTMLMemo1, UniHTMLMemo1.JSInterface do Begin
    JSCode('Ext.defer(function(){ajaxRequest(' + JSName + ', "getSelection", ["selectedText="+' + JSName + '.getDoc().getCursor().ch])}, 0);')
  end;

 

Link to comment
Share on other sites

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...