Keyboard shortcuts with javascript
Moderator: General Moderators
Keyboard shortcuts with javascript
Is there a way to show/hide a table with a keyboard shortcut using javascript?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
adamb10 wrote:Ok. I dont really know much(if any) javascript.
Code: Select all
function readKey(e)
{
if (e.keyCode == 46) alert('Delete pressed!');
}
if (document.captureEvents) document.captureEvents(Event.KEYDOWN);
document.onkeydown = readKey;
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
No, let's say you wanted the "h" key to toggle the table display.adamb10 wrote:ok, and what do I put in the table tag? Readkey?
Code: Select all
function toggleTable(e)
{
e = e || window.event;
if (e.keyCode == 72) //Key code for h
{
var tableElement = document.getElementById('my_table');
if (tableElement.style.display == 'block') tableElement.style.display = 'none';
else tableElement.style.display = 'block';
}
}
if (document.captureEvents) document.captureEvents(Event.KEYDOWN);
document.onkeydown = toggleTable;Code: Select all
<table id="my_table" ... >Thanks. That appeared to have worked. It does have a few bugs though. When you press h, the table gets skinny then pressing h again makes it go away. Pressing h again makes the table skinny.
http://adamb10.com/pb2/
Thanks!
http://adamb10.com/pb2/
Thanks!
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Code: Select all
function toggleTable(e)
{
e = e || window.event;
if (e.keyCode == 72) //Key code for h
{
var tableElement = document.getElementById('my_table');
if (tableElement.style.display == '') tableElement.style.display = 'none';
else tableElement.style.display = '';
}
}
if (document.captureEvents) document.captureEvents(Event.KEYDOWN);
document.onkeydown = toggleTable;