Re: Background Keystroke Commands...
- From: fajp@xxxxxxxxxxxxxxxxxxxx (Frank Adam)
- Date: Tue, 26 Jul 2005 07:50:42 +1000
On 24 Jul 2005 13:09:21 -0700, "cwizzie" <cwizze@xxxxxxxxx> wrote:
>This is my problem...
>
>I'm creating a program that should not be able to close the user, so I
>have disabled the close button "x" on the upper right window corner. I
>have also not programmed any close buttons within the program's window
>space.
>
>What I would like to do...
>
>I would like it so that when the user hits a series of keys on the
>keyboard it opens a conformation box and gives the option of closing or
>not.
>
>Could someone tell me how to go about this?
>
I see no one actually answered the question, so here is one way to do
what you want. Actually two ways.
Paste this into a form and make sure keypreview is set to true. If you
want, add a textbox too to test the textbox code, keypreview does not
matter for the latter.
Option Explicit
'for use with form_keypress
Private Const C_PASS = "hello1"
'for use with textbox
Private Const C_PASS1 = "chello"
'The form's keypreview must be set to true for this.
'This looks at the last six keys pressed, adjust for longer strings
Private Sub Form_KeyPress(KeyAscii As Integer)
Static keyPrev(1 To 6) As Byte 'store the keys here, circular
Static pos As Long 'counter
pos = pos + 1
If pos > 6 Or KeyAscii = vbKeyEscape Then
pos = 0 'pressing escape resets the queue to 0
Else
keyPrev(pos) = KeyAscii 'store the key
If pos = 6 Then 'if we are at 6, we compare
If StrConv(keyPrev, vbUnicode) = C_PASS Then
If MsgBox("Shut down ?", vbYesNo, "Huh ?") = _
vbYes Then
Unload Me
End If
Else
pos = 0 'reset
End If
End If
End If
End Sub
' If you have a textbox anywhere on the form, then you can use this
' instead. Type the password into the textbox and hold CTRL+SHIFT,
' while clicking the left mouse button. Most users wouldn't think of
' doing something like that.
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If Button = vbLeftButton And (Shift And vbCtrlMask) = vbCtrlMask _
And (Shift And vbShiftMask) = vbShiftMask Then
If Text1.Text = C_PASS1 Then
If Msgbox("Shut down ?",vbYesNo,"Huh ?") = vbYes then
Unload me
End If
End If
End If
End Sub
HTH
--
Regards, Frank
.
- Follow-Ups:
- Re: Background Keystroke Commands...
- From: Steve Gerrard
- Re: Background Keystroke Commands...
- References:
- Background Keystroke Commands...
- From: cwizzie
- Background Keystroke Commands...
- Prev by Date: Re: vb newbie help
- Next by Date: Re: Background Keystroke Commands...
- Previous by thread: Re: Background Keystroke Commands...
- Next by thread: Re: Background Keystroke Commands...
- Index(es):