-
Notifications
You must be signed in to change notification settings - Fork 12
Create a Custom KeyRelease Function Example
Andreas Justin edited this page Aug 22, 2019
·
16 revisions
FYI: I've written this without Matlab. I did not have the time to test this yet. If there is a bug (highly probable) I'm sorry, see if you can fix it, if so write me an email with the fix and i'll update this page
import at.mep.util.*;
import at.mep.editor.*;
ctrl = true;
shift = true;
alt = true;
% Move Lines Down
ks_MoveLinesDown = KeyStrokeUtil.getKeyStroke(java.awt.event.KeyEvent.VK_DOWN, ~ctrl, shift, ~alt, false);
EditorApp.addMatlabCallback('ks_MoveLinesDown_function', ks_MoveLinesDown, 'ks_MoveLinesDown_function');
% Move Lines UP
ks_MoveLinesUp = KeyStrokeUtil.getKeyStroke(java.awt.event.KeyEvent.VK_UP, ~ctrl, shift, ~alt, false);
EditorApp.addMatlabCallback('ks_MoveLinesUp_function', ks_MoveLinesUp, 'ks_MoveLinesUp_function');
function out = ks_MoveLinesUp_function(actionEvent)
if nargin == 0; out = []; return; end
% this is necessary, the first thing happen is trying find out whether
% the passed function is a valid matlab function or not
%
% I strongly advise you not to debug these functions via keyboard command (matlab will most likely freeze).
% Instead call it from command line. If you need access to `actionEvent` use `assignin('base','actionEvent',actionEvent)`
% The currently active Editor where Matlab's focus is on.
editor = at.mep.editor.EditorWrapper.getActiveEditor();
% get line above
% --------------------------------------------------------------
% you may have to convert an java int[] to an matlab double vector (1xN)
se = at.mep.editor.EditorWrapper.getCurrentLinesStartEnd(editor);
lineAbove = se(1) - 1;
if (lineAbove < 1)
% in First line, cannot go any further above
return;
end
% get lines to move
% --------------------------------------------------------------
% you might have to convert a java String[] into an Matlab String or Cell-Character array
strings = string(at.mep.editor.EditorWrapper.getTextArrayFast(editor));
anzStrings = se(2) - se(1) + 1 + 1;
strings2Insert = strings(anzStrings,1);
for ii = 1:anzStrings
% might have introduced an Off-By-One-Error
strings2Insert(ii) = strings(lineAbove-1 + ii);
end
% move line above below others
% --------------------------------------------------------------
strings2Insert$(first empty entry) = strings2Insert; % .remove(0) ... i don't know what this does in the moment, remove the first character?
% replace new ordered
posS = at.mep.editor.EditorWrapper.lc2pos(editor, lineAbove, 0);
% intmax() might need to be adjusted
posE = at.mep.editor.EditorWrapper.lc2pos(editor, lineAbove + anzStrings - 1, intmax()) + 1;
at.mep.editor.EditorWrapper.setSelectionPosition(editor, posS, posE);
at.mep.editor.EditorWrapper.setSelectedTxt(editor, String.join("", strings2Insert));
% select same lines as before
% --------------------------------------------------------------
posS = at.mep.editor.EditorWrapper.lc2pos(editor, lineAbove, 0);
% intmax() might need to be adjusted
posE = at.mep.editor.EditorWrapper.lc2pos(editor, lineAbove + anzStrings - 2, intmax);
at.mep.editor.EditorWrapper.setSelectionPosition(editor, posS, posE);
Contact: [email protected]