How to capture and then cancel Tab key event.

How to capture and then cancel Tab key event.

Post by Hopf » Wed, 10 Jan 2007 07:12:58


The default action of tab key is to move focus.
For example,
<textarea id="ta"></textarea>
<textarea id="tb"></textarea>

Pressing Tab key when cursor is in the first textarea results in cursor
in the second textarea.
Is there a cross-browser way to capture the event of pressing Tab key
in a textarea and canceling the focus-moving action?

I tried the following code:

<textarea id="ta"></textarea>
<textarea id="tb"></textarea>
<script>
document.getElementById('ta').onkeypress = function(e){
e = e || window.event;
var code = e.keyCode || e.which;
var c = String.fromCharCode(code);
if(c == '\t'){
alert('TAB pressed'); return false;
} else return true;
}
</script>

And I put cursor in the first textarea and pressed Tab.
IE 7 showed no alertbox and focus moved. not ok.
Firefox 2.0 showed alertbox and focus stayed. ok.
Opera 9 showed alertbox and focus moved. not ok.
 
 
 

1. Capturing a TAB key press event

2. how do we capture the Tab Key Press event in a text box

'By default, VB uses the Tab key to move focus between controls.
'To capture the tab key you will need to use
'windows API GetAsyncKeyState

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vVirtKey
As Long) As Integer


Private Function KeyPressed(iKey As Integer) As Boolean
If GetAsyncKeyState(iKey) < 0 Then
KeyPressed = True
Else
KeyPressed = False
End If
End Function

Private Sub txtTab_LostFocus()
If KeyPressed(vbKeyTab) = True Then
MsgBox "TAB Key Pressed"
End If
End Sub

3. Problem capturing the Tab key in the KeyDown event

4. Capturing key press/release events on keyboard for all keys in VC+

5. TAB KEY working together KEY PRESS event???

6. DetailsView - how to capture the click event on Edit and Cancel command?

7. Capture FormView Cancel Event

8. Capturing the Cancel event of Print Dialog

9. Capture a Cancel Event

10. capturing messagebox OK/cancel event

11. How to capture Cancel event of a PropertySheet

12. Capturing the Cancel Event on a userform

13. How to catch and cancel pressing Tab key ?

14. How to cancel pressing the Tab key?