Get the location of writing cursor (keyboard) in text area of app designer

31 次查看(过去 30 天)
I am creating a simple text editor. I just want to get the location of writing cursor inside the text area so that I can insert some text in this location when clicking a button. Is there any method to get which row and column inside the text area?

回答(1 个)

Harsha Vardhan
Harsha Vardhan 2024-1-1
Hi,
I understand that you want to insert text next to the existing text when a button is clicked.
This can be done by appending new text to the existing text as below.
I created a textbox and button as shown below.
Whenever the 'Button' is clicked, the current time is appened next to the existing text in the textbox.
Please check callback function associated with Button click below. This function appends next text whenever 'Button' is clicked.
% Button pushed function: Button
function ButtonPushed(app, event)
% Get the current time as a string
currentTime = char(datetime('now', 'Format', 'HH:mm:ss'));
% Check if the TextArea is empty
if isempty(app.TextArea.Value)
% If empty, add the current time without a comma
newContent = currentTime;
else
% If not empty, append the current time with a comma and space
newContent = [char(app.TextArea.Value), ', ', currentTime];
end
% Update the TextArea with the new content
app.TextArea.Value = newContent;
end
Similarly, one more use case is below. Whenever, the button is clicked a new number is appended to the text box, which counts the number of times the button was clicked. And this time, there is no space or comma between these numbers.
Please check the screenshot of the App below.
Add a property called ButtonClickCount as below.
properties (Access = public)
ButtonClickCount double = 0; % New property to track button clicks
end
Please check callback function associated with Button click below. This function appends next text whenever 'Button' is clicked.
% Button pushed function: Button
function ButtonPushed(app, event)
% Increment the button click count
app.ButtonClickCount = app.ButtonClickCount + 1;
% Convert the count to string
countStr = num2str(app.ButtonClickCount);
% Append the count to the existing content of the TextArea
newContent = [char(app.TextArea.Value), countStr];
% Update the TextArea with the new content
app.TextArea.Value = newContent;
end
Hope this helps in resolving your query!
  2 个评论
Osama El-Ghonimy
Osama El-Ghonimy 2024-1-2
Thanks for your help
However, I need to insert a text in the middle of the text area where the cursor is found (not appending to the end). So, the user moves the cursor to some position and clicks a button and it inserts some text at this position.
Harsha Vardhan
Harsha Vardhan 2024-1-3
编辑:Harsha Vardhan 2024-1-3
Hi @Osama El-Ghonimy, I explored few options.
Multiple MATLAB answers suggest to manipulate the internal Java objects for the GUIDE based textbox.
  1. https://in.mathworks.com/matlabcentral/answers/415003-how-to-move-cursor-line-within-edit-box-of-uicontrol#answer_332885
  2. https://in.mathworks.com/matlabcentral/answers/271058-how-to-determine-cursor-position-in-edit-uicontrol#answer_212034
  3. https://in.mathworks.com/matlabcentral/answers/10993-set-cursor-position-in-multiline-uicontrol-edit-box#answer_15052
App Designer does not support direct control of cursor position in MATLAB R2023b. So, you can try either of these methdods.
Method 1: Using 'CurrentPoint' and 'Position' Properties
This method involves getting the 'CurrentPoint' property from the figure and comparing it against the 'Position' property of the edit box to estimate the cursor's position. However, this approach has a significant limitation: it doesn't precisely determine the cursor's position within the text but rather gives a rough estimate based on the mouse position. This method is not commonly used for text editors as it lacks accuracy.
Method 2: Using 'FindJObj' (https://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects ) to Access Java Object
Note: 'FindJObj' is a 3rd party library not supported by MathWorks.This approach requires the Java Swing components underlying MATLAB's GUI elements.
The 'FindJObj' utility can be used to access the underlying Java object of MATLAB GUI components, offering more control. Here’s how you can use it:
  1. Install 'FindJObj': If you don't already have 'FindJObj', you can download it from the MATLAB File Exchange - https://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects
  2. Access the Java Object: Use 'FindJObj' to get the Java object of the text edit component.
  3. Get and Set Cursor Position: Use Java methods to get the current cursor position and insert text.
Please find sample code below.
function multiLineTextEditor
% Create a figure and text edit box
hFig = figure;
hEdit = uicontrol('Style', 'edit', 'Max', 10, 'Position', [20 100 260 100]);
% Create a button to insert text
hButton = uicontrol('Style', 'pushbutton', 'String', 'Insert Text', ...
'Position', [20 50 100 30], 'Callback', @insertText);
% Use FindJObj to access the Java object of the edit box
jhEdit = findjobj(hEdit);
jEdit = jhEdit.getComponent(0).getComponent(0);
function insertText(~, ~)
% Get the current caret (cursor) position
caretPos = jEdit.getCaretPosition;
% Define the text to insert
newText = 'Hello MATLAB!';
javaCurrentText = jEdit.getText;
mlCurrentText = char(javaCurrentText);
% Insert new text at the caret position
updatedText = [mlCurrentText(1:caretPos), newText, mlCurrentText(caretPos+1:end)];
% Update the text in the edit box
set(hEdit, 'String', updatedText);
end
end
Using the above code, though the text gets inserted at the cursor, a problem persists. In each new line of the textbox, wherever the cursor may be, the first insertion using the button is always at the right most point. Later insertions are working correctly at the cursor's location. You may explore further into this.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by