How to scroll by code in the terminal ?

0 votes
asked Dec 11, 2012 by Andre (650 points)
edited Jul 29, 2013

I need to implement shortcuts to scroll in Telnet/SSH Terminal. How can I force scroll Up/Down (full page, half page and one line at a time) ?

Thank you

André Sanscartier

1 Answer

+1 vote
answered Dec 12, 2012 by Lukas Matyska (44,590 points)
edited Jul 29, 2013
 
Best answer

There is no method for programmatic scrolling of TerminalControl in the current version, but you can use a workaround using OnMouseWheel method. It can be done like this:

public class MyTerminalControl : TerminalControl
{
    private const int WHEEL_DELTA = 120;

    /// <summary>
    /// Scrolls the control.
    /// </summary>
    /// <param name="rows">Number of rows to scroll.</param>
    public void Scroll(int rows)
    {
        MouseEventArgs e = new MouseEventArgs(0, 0, 0, 0, rows * WHEEL_DELTA);
        base.OnMouseWheel(e);
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        int delta = 0;
        switch (e.KeyCode)
        {
            case Keys.PageUp:
                if (e.Control)
                    delta = 1;
                if (e.Shift)
                    delta = Screen.Rows;
                break;
            case Keys.PageDown:
                if (e.Control)
                    delta = -1;
                if (e.Shift)
                    delta = -Screen.Rows;
                break;
        }
        if (delta != 0)
        {
            Scroll(delta);
            return;
        }

        base.OnKeyDown(e);
    }
}

On the form use the MyTerminalControl instead of the TerminalControl. Now you are able to scroll by hitting Ctrl+PageUp/Down (one line) and by hitting Shift+PageUp/Down (one screen).

commented Dec 13, 2012 by Andre (650 points)
edited Dec 13, 2012

Great! Works like a charm :)

commented Jul 29, 2013 by Lukas Matyska (44,590 points)
edited Jul 29, 2013

From version 2013 R1 (build number 4959), you can use public method Scroll(int rows).

...