Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBScript disable Alt + Home

Posted on 2004-08-20
18
Medium Priority
?
2,145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
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 800 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
 

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 200 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

664 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