Solved

VBScript disable Alt + Home

Posted on 2004-08-20
18
2,077 Views
Last Modified: 2010-08-05
Before anyone asks why you want to do this, this is for an in-house web application, so we want to prevent users from doing things that will put their application into a weird state.  So that being said, I have been able to disable all the IE keyboard shortcuts, all except for the Alt + Home key.  Basically I'm doing everything onKeyDown, and I've noticed that for single key presses you have to set the windows.event.returnValue to false and the window.event.keyCode to 0 as well.  But some key combinations all you need to do is set the window.event.returnValue to false.  Is there something else that you need to do to "cancel" the Alt + Home combination?

Thanks in advance,

Chris
0
Comment
Question by:chriskchan
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 10

Expert Comment

by:avidya
ID: 11854774
0
 

Author Comment

by:chriskchan
ID: 11854819
Thanks for the reply, but I have already seen that, and it's Javascript, not VBScript.  I have disabled everything else, e.g. Ctrl +N, F5, Ctrl+R, Backspace, Alt + Left, Alt + Right, etc., but I can't figure out how to disable Alt + Home.  There must be something else that you have to do to disable it.
0
 
LVL 10

Expert Comment

by:avidya
ID: 11854865
sorry, the one above is java, these are vbs:
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20903665.html

Tis is also java which you didn't request, but it works:
===============
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script>
 function NoIEKey()
 {
  event.keyCode=0
  return false
 }
 document.onkeydown=NoIEKey
 window.onhelp=function(){return false};
</script>
</HEAD>
<BODY>

</BODY>
</HTML>
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11854874
Hmm ... I did something similar to this in javascript, but haven't done this with VBscript.  I set the page up to "alert()" me with the keycode each time a key was pressed, so I could grab the codes for each key combination, and used the info to restrict input.

Have you tried something like that in VBscript ?
0
 
LVL 10

Expert Comment

by:avidya
ID: 11854878
sorry, didn't read your question good enough, will try to disable alt home for you.
0
 

Author Comment

by:chriskchan
ID: 11854940
Gitcho, thanks for the reply but yes I already know the key combination for Alt + Home, but even though I can capture it, I can't disable that combination, even by setting the window.event.returnValue to false and the window.event.keyCode to 0.  
0
 
LVL 10

Expert Comment

by:avidya
ID: 11855063
chrishchan, could you post your code?
0
 

Accepted Solution

by:
john_horn1234 earned 400 total points
ID: 11855096
Weird...

If you put an alert in it won't redirect you to the homepage:

<SCRIPT language="vbscript">

Function onKeyDown()
      If (window.event.keycode=36) And (window.event.altkey)Then
            call alert("Alt-Home Pressed")            
      End If
End Function

</SCRIPT>

<body onkeydown="call onKeyDown()">
</body>
0
 

Author Comment

by:chriskchan
ID: 11855209
Here is my code for the rest:

function document_onKeyDown
                  dim bAltKey
                  dim bCtrlKey
                  dim bShiftKey
                  dim kpressed
                  dim srcElementTagName
                  dim bDisableBackspace

                  kpressed = window.event.keyCode
                  srcElementTagName = window.event.srcElement.tagName
                  if (srcElementTagName = "INPUT") then
                        bDisableBackspace = false
                  else
                        bDisableBackspace = true
                  end if

                  if window.event.altKey = true then
                        bAltKey = true
                  else
                        bAltKey = false
                  end if

                  if window.event.ctrlKey = true then
                        bCtrlKey = true
                  else
                        bCtrlKey = false
                  end if

                  if window.event.shiftKey = true then
                        bShiftKey = true
                  else
                        bShiftKey = false
                  end if
                  window.status = "key pressed code = " & kpressed & " altkey=" & bAltKey & " ctrlkey=" & bCtrlKey
                  'Detect for single key presses
                  'F4,F5,F6,F11, extended windows keyboard "Back" and "Forward", backspace (as long as it's not in an input field), Ctrl + O (this one is special and needs to set the keycode to 0), Escape, (Alt + Home)
                  if ((kpressed = 116) or (kpressed = 115) or (kpressed = 117) or (kpressed = 122) or (kpressed = 166) or (kpressed = 167) or (kpressed = 8 and bDisableBackspace) _
                  or (kpressed = 79 and bCtrlKey) or (kpressed = 27)) then
                        window.event.keyCode = 0
                        window.event.returnValue = false
                  'detect for combination key presses (Alt + Left Arrow) and (Alt + Right Arrow), (Ctrl + N), (Ctrl + L), (Shift + F10), (Ctrl + R), (Ctrl + E), (Ctrl + I), (Ctrl + H)
                  elseif ((kpressed = 37 and bAltKey) or (kpressed = 39 and bAltKey) or (kpressed = 78 and bCtrlKey) or (kpressed = 76 and bCtrlKey) _
                  or (kpressed = 121 and bShiftKey) or (kpressed = 82 and bCtrlKey) or (kpressed = 69 and bCtrlKey) or (kpressed = 73 and bCtrlKey) _
                  or (kpressed = 72 and bCtrlKey) or (kpressed = 9 and bCtrlKey)) then
                        window.event.returnValue = false
                  elseif ((kpressed = 36 and bAltKey)) then
                        window.event.keyCode = 0
                        window.event.returnValue = false
                        window.event.altKey = false
                  end if
            end function

The last elseif is for the Alt + Home combination, but it's not working.  I was trying to set the altKey to false too but that didn't do anything.

Chris
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:chriskchan
ID: 11855260
Hey John you're right, that's weird, popping up an alert does the trick.  But I wonder if there's a way to do it without popping up a message?  The alert must cancel something that the keyCode = 0 and returnValue = false is not?
0
 
LVL 10

Assisted Solution

by:avidya
avidya earned 100 total points
ID: 11855296
altkey is a read-only property and is TRUE if the key is down, or FALSE otherwise, so that doesn't work, nice one this is, puzzle along
0
 

Author Comment

by:chriskchan
ID: 11855532
setting the cancelBubble to false doesn't seem to do anything either... hmmm
0
 

Author Comment

by:chriskchan
ID: 11855538
Woops I mean cancelBubble to true
0
 
LVL 10

Expert Comment

by:avidya
ID: 11855599
*grin*, just fiound that out too.
Seems to have something to di with fireing the event
0
 

Expert Comment

by:john_horn1234
ID: 11855613
Looks like its either an IE bug or a feature (depending how you look at it...)

Here's the best solution I could come up with (Its a major hack but it might meet your requirements):


<SCRIPT language="vbscript">
function onKeyDown()
      If (window.event.keycode=36) And (window.event.altkey) Then
            window.open("","","height=1,width=1,top=1,left=1").close
            call window.setTimeout("window.focus",1)
      End If
End Function
</SCRIPT>

<body onkeydown="onKeyDown()"></body>

0
 
LVL 10

Expert Comment

by:avidya
ID: 12115383
Hi,

Would you be so kind to respond to the suggestions?
0
 

Author Comment

by:chriskchan
ID: 12115551
Sure, well I haven't been able to find anything better than John's alert popup to disable the Alt+Home combination.  So I'm just using that method if they hit the Alt+Home keys.  All the other "forbidden" key combinations just get disabled silently.
0
 
LVL 10

Expert Comment

by:avidya
ID: 12115617
Hi Chriskchan,

Thanks for the update!
(i'm still searching on the alt-home disable, nothing so far..
If I ever find it, I will let you know.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Read about why website design really matters in today's demanding market.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now