# Comment/Uncomment code shortcut key

**URL:** https://forum.alternetsoft.com/t/comment-uncomment-code-shortcut-key/478
**Category:** AlterNET Studio Support
**Created:** [February 4, 2025, 4:42pm UTC](https://forum.alternetsoft.com/t/comment-uncomment-code-shortcut-key/478 "2025-02-04T16:42:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rob.panosh](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/r/606060/32.png) [@rob.panosh](https://forum.alternetsoft.com/u/rob.panosh)
#### Post date: [February 4, 2025, 4:42pm UTC](https://forum.alternetsoft.com/t/comment-uncomment-code-shortcut-key/478/1 "2025-02-04T16:42:37Z")

</div>

I would like to add a hotkey to the SyntaxEditor for toggling comment/uncomment of highlighted text. For example, VS Code uses Ctrl+“/” and I would like to do the same.

Thanks,  
Rob

---

<div class="post-metadata">

### Author: ![andrew.medvedev](https://forum.alternetsoft.com/user_avatar/forum.alternetsoft.com/andrew.medvedev/32/28_2.png) [@andrew.medvedev](https://forum.alternetsoft.com/u/andrew.medvedev)
#### Post date: [February 5, 2025, 1:09am UTC](https://forum.alternetsoft.com/t/comment-uncomment-code-shortcut-key/478/2 "2025-02-05T01:09:22Z")

</div>

Hi Rob,

You should use KeyList.Add functionality to customize shortcut keys like this:

```auto
syntaxEdit1.KeyList.Add(Keys.Oem2 | Keys.Control, new Alternet.Editor.KeyEvent(ToggleComment), 0, 0);
        protected bool CanUncommentSelection(string start, string end)
        {
            var selRect = syntaxEdit1.Selection.SelectionRect;
            string s = syntaxEdit1.Lines[selRect.Top].TrimStart();

            if (!s.StartsWith(start))
                return false;
            s = syntaxEdit1.Lines[selRect.Right == 0 ? selRect.Bottom - 1 : selRect.Bottom].TrimEnd();
            return s.EndsWith(end);
        }

        void ToggleComment()
        {
            bool comment = true;
            var parser = syntaxEdit1.Lexer as ISyntaxParser;
            if (parser != null)
            {
                string start;
                string end;
                if (parser.GetMultiLineComment(syntaxEdit1.Position, out start, out end))
                {
                    if (CanUncommentSelection(start, end))
                    {
                        comment = false;
                    }
                }
                else
                {
                    var commentStr = parser.GetSingleLineComment();
                    var pos = syntaxEdit1.Selection.IsEmpty ? syntaxEdit1.Position.Y : syntaxEdit1.Selection.SelectionRect.Top;
                    if (!string.IsNullOrEmpty(commentStr))
                    {
                        var str = syntaxEdit1.Lines[pos].Trim();
                        if (str.StartsWith(commentStr))
                            comment = false;
                    }
                }
            }

            if (comment)
                syntaxEdit1.Selection.CommentSelection();
            else
                syntaxEdit1.Selection.UncommentSelection();
        }

```

Regards,  
Andrew

---

<div class="post-metadata">

### Author: ![rob.panosh](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/r/606060/32.png) [@rob.panosh](https://forum.alternetsoft.com/u/rob.panosh)
#### Post date: [February 5, 2025, 9:09pm UTC](https://forum.alternetsoft.com/t/comment-uncomment-code-shortcut-key/478/3 "2025-02-05T21:09:28Z")

</div>

Thanks Andrew! I will give that a try.

---

<div class="post-metadata">

### Author: ![rob.panosh](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/r/606060/32.png) [@rob.panosh](https://forum.alternetsoft.com/u/rob.panosh)
#### Post date: [February 6, 2025, 1:58pm UTC](https://forum.alternetsoft.com/t/comment-uncomment-code-shortcut-key/478/4 "2025-02-06T13:58:06Z")

</div>

Thanks, it works perfectly.

---

<div class="post-metadata">

### Author: ![FOstyn](https://forum.alternetsoft.com/letter_avatar_proxy/v4/letter/f/606060/32.png) [@FOstyn](https://forum.alternetsoft.com/u/FOstyn)
#### Post date: [February 12, 2025, 6:33am UTC](https://forum.alternetsoft.com/t/comment-uncomment-code-shortcut-key/478/5 "2025-02-12T06:33:04Z")

</div>

@Andrew,  
the CTRL+/ is also a recent update in Visual Studio 2022, maybe an idea to implement that default in the SyntaxEditor, as an **alter** native to CTRL+e, c and or CTRL+e,u ?

---

<div class="post-metadata">

### Author: ![andrew.medvedev](https://forum.alternetsoft.com/user_avatar/forum.alternetsoft.com/andrew.medvedev/32/28_2.png) [@andrew.medvedev](https://forum.alternetsoft.com/u/andrew.medvedev)
#### Post date: [February 19, 2025, 2:48am UTC](https://forum.alternetsoft.com/t/comment-uncomment-code-shortcut-key/478/6 "2025-02-19T02:48:51Z")

</div>

Hi,

We will add this for the next version.

Regards,  
Andrew

---

<div class="post-metadata">

### Author: ![dmitry.medvedev](https://forum.alternetsoft.com/user_avatar/forum.alternetsoft.com/dmitry.medvedev/32/27_2.png) [@dmitry.medvedev](https://forum.alternetsoft.com/u/dmitry.medvedev)
#### Post date: [February 27, 2025, 11:07pm UTC](https://forum.alternetsoft.com/t/comment-uncomment-code-shortcut-key/478/7 "2025-02-27T23:07:33Z")

</div>

Hi Rob,

We’ve just released an update ([9.5.13](https://www.alternetsoft.com/version-history/9-5-x)), where these shortcuts are added by default

Kind regards,  
Dmitry
