<p>I would like to be able to use the <kbd>Tab</kbd> key within a text box to tab over four spaces. The way it is now, the Tab key jumps my cursor to the next input.</p>

<p>Is there some JavaScript that will capture the Tab key in the text box before it bubbles up to the UI?</p>

<p>I understand some browsers (i.e. FireFox) may not allow this. How about a custom key-combo like <kbd>Shift</kbd>+<kbd>Tab</kbd>, or <kbd>Ctrl</kbd>+<kbd>Q</kbd>?</p>

[{'answer_id': 3370, 'author': 'Tom', 'author_id': 20, 'author_profile': 'https://Stackoverflow.com/users/20', 'pm_score': 4, 'selected': False, 'text': "<p>The previous answer is fine, but I'm one of those guys that's firmly against mixing behavior with presentation (putting JavaScript in my HTML) so I prefer to put my event handling logic in my JavaScript files. Additionally, not all browsers implement event (or e) the same way. You may want to do a check prior to running any logic:</p>\n\n<pre><code>document.onkeydown = TabExample;\n\nfunction TabExample(evt) {\n  var evt = (evt) ? evt : ((event) ? event : null);\n  var tabKey = 9;\n  if(evt.keyCode == tabKey) {\n    // do work\n  }\n}\n</code></pre>\n"}, {'answer_id': 3371, 'author': 'Lauren', 'author_id': 265, 'author_profile': 'https://Stackoverflow.com/users/265', 'pm_score': 4, 'selected': False, 'text': "<p>I'd rather tab indentation not work than breaking tabbing between form items.</p>\n\n<p>If you want to indent to put in code in the Markdown box, use <kbd>Ctrl</kbd>+<kbd>K</kbd> (or ⌘K on a Mac).</p>\n\n<p>In terms of actually stopping the action, jQuery (which Stack Overflow uses) will stop an event from bubbling when you return false from an event callback. This makes life easier for working with multiple browsers.</p>\n"}, {'answer_id': 3372, 'author': 'Wally Lawless', 'author_id': 37, 'author_profile': 'https://Stackoverflow.com/users/37', 'pm_score': 3, 'selected': False, 'text': '<p>I would advise against changing the default behaviour of a key.  I do as much as possible without touching a mouse, so if you make my tab key not move to the next field on a form I will be very aggravated.</p>\n\n<p>A shortcut key could be useful however, especially with large code blocks and nesting.  Shift-TAB is a bad option because that normally takes me to the previous field on a form.  Maybe a new button on the WMD editor to insert a code-TAB, with a shortcut key, would be possible?</p>\n'}, {'answer_id': 13130, 'author': 'ScottKoon', 'author_id': 1538, 'author_profile': 'https://Stackoverflow.com/users/1538', 'pm_score': 8, 'selected': True, 'text': '<p>Even if you capture the <code>keydown</code>/<code>keyup</code> event, those are the only events that the tab key fires, you still need some way to prevent the default action, moving to the next item in the tab order, from occurring.</p>\n\n<p>In Firefox you can call the <code>preventDefault()</code> method on the event object passed to your event handler. In IE, you have to return false from the event handle. The JQuery library provides a <code>preventDefault</code> method on its event object that works in IE and FF.</p>\n\n<pre><code>&lt;body&gt;\n&lt;input type="text" id="myInput"&gt;\n&lt;script type="text/javascript"&gt;\n    var myInput = document.getElementById("myInput");\n    if(myInput.addEventListener ) {\n        myInput.addEventListener(\'keydown\',this.keyHandler,false);\n    } else if(myInput.attachEvent ) {\n        myInput.attachEvent(\'onkeydown\',this.keyHandler); /* damn IE hack */\n    }\n\n    function keyHandler(e) {\n        var TABKEY = 9;\n        if(e.keyCode == TABKEY) {\n            this.value += "    ";\n            if(e.preventDefault) {\n                e.preventDefault();\n            }\n            return false;\n        }\n    }\n&lt;/script&gt;\n&lt;/body&gt;\n</code></pre>\n'}, {'answer_id': 2280003, 'author': 'Paul D. Waite', 'author_id': 20578, 'author_profile': 'https://Stackoverflow.com/users/20578', 'pm_score': 2, 'selected': False, 'text': '<p>In Chrome on the Mac, alt-tab inserts a tab character into a <code>&lt;textarea&gt;</code> field.</p>\n\n<p>Here’s one: . Wee!</p>\n'}, {'answer_id': 3674216, 'author': 'chintan123', 'author_id': 443119, 'author_profile': 'https://Stackoverflow.com/users/443119', 'pm_score': 2, 'selected': False, 'text': "<p>there is a problem in best answer given by ScottKoon</p>\n\n<p>here is it</p>\n\n<pre><code>} else if(el.attachEvent ) {\n    myInput.attachEvent('onkeydown',this.keyHandler); /* damn IE hack */\n}\n</code></pre>\n\n<p>Should be </p>\n\n<pre><code>} else if(myInput.attachEvent ) {\n    myInput.attachEvent('onkeydown',this.keyHandler); /* damn IE hack */\n}\n</code></pre>\n\n<p>Due to this it didn't work in IE.\nHoping that ScottKoon will update code</p>\n"}]
['https://Stackoverflow.com/questions/3362', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/357/']