How to do a psuedo-macro

SyntaxEditor for Windows Forms Forum

Posted 19 years ago by NSXDavid
Avatar
So I have this situation, I need it such that when a user presses Alt+ENTER it acts as if they had pressed ENTER followed by the period key, followed by the up arrow followed by the END key. The period key unindents on a trigger.

So if the code looked like this:

  if bla then|
  printsomething
(Where | is where the cursor is) and they press ALT+Enter, we get:

  if bla then
    |
  .
  printsomething
(If smartindents and . triggers an unindent, again | is where I wnat the cursor to be).

I haven't figured out how to do this. My first attempt was to catch the ALT+ENTER, which is easy. But then what to do? I issue a SendKeys.SendWait("{ENTER}.{UP}{END}"); but unfortunately the ALT key is still being held down and so the {ENTER} key comes back through as ALT+ENTER. That leads to an infinite loop. If I avoid the infinite loop with a bool it still doesn't work because ALT+ENTER != ENTER as far as SyntaxEditor is concerned.

Any ideas how to make this happen?

-- David

Comments (8)

Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
What we do with all our hotkeys is add a CommandLink to the SyntaxEditor.CommandLinks collection. Make sure you remove any default one for Alt+Enter before you add your own. Then you have the CommandLink point to an EditCommand that you make, which implements an Execute method. In the Execute method you can change selection, insert text, etc. That's how we implement all our keyboard commands.


Actipro Software Support

Posted 19 years ago by NSXDavid
Avatar
Is there a sample of this anywhere? The docs are pretty sparse on this I think.

-- David
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Sure... here's a sample command:
/// <summary>
/// Inserts a line break at the current position.
/// </summary>
public class InsertLineBreakCommand : EditCommand {

    /// <summary>
    /// Executes the edit command.
    /// </summary>
    /// <param name="context">An <see cref="EditCommandContext"/> that holds context-related information.</param>
    public override void Execute(EditCommandContext context) {
        // Ensure the selection's view is active
        context.Selection.View.Focus();

        // Quit if read-only
        if (context.Document.ReadOnly) return;

        // Insert line break
        context.View.PerformInsertTyping('\n');
    }
}
And this is how you create a CommandLink for it:
new CommandLink(new InsertLineBreakCommand(), new KeyBinding(WinUICore.Input.ModifierKeys.None | WinUICore.Input.ModifierKeys.Shift | WinUICore.Input.ModifierKeys.ControlShift, Keys.Enter))


Actipro Software Support

Posted 19 years ago by NSXDavid
Avatar
Thanks... that worked. Interestingly the typing of a '.' via the PerformInsertTyping() doesn't hit the trigger, so I had to simulate the intended outdent in the command itself too.

Is that normal?

-- David
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Right now triggers are firing off from the OnKeyPress override in SyntaxEditor. The reason is that the PerformInsertTyping is not called when the Document is ReadOnly, however some triggers can still be executed when the Document is ReadOnly.


Actipro Software Support

Posted 19 years ago by NSXDavid
Avatar
I don't follow. Shouldn't PerformTyping, as the name implies, behave like typing? ReadOnly isn't an issue here, is it?

-- David
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
You're probably right, we should rearrange the calls so that that method truly does behave as it a KeyPress occurred.


Actipro Software Support

Posted 19 years ago by NSXDavid
Avatar
<IMG SRC="biggrin.gif" border="0"> Cool... although when you do, my code will double unindent... but I can fix that easily enough. Thanks!

-- David
The latest build of this product (v24.1.0) was released 3 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.