Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trap for Print Screen button 1

Status
Not open for further replies.

DevonTaig

Programmer
May 2, 2001
73
US
I would like to prohibit the user from using the Print Screen button while running my application. The Keypress, and KeyDown events don't seem to do the trick on that particular button. Any ideas on how to accomplish this? Thanks.
 
I think you'd need to use the KeyUp event. KeyCode 44 would appear to be the Print Screen button.

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Or you can use the following (which was adapted from Hypetia's wonderful example in thread222-607295)

In a module:
Code:
Option Explicit
Public Const GWL_WNDPROC = (-4)
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const MOD_ALT = &H1
Public Const MY_HOTKEY = &H1000&
Public Const WM_HOTKEY = &H312
Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public lpPrevWndProc As Long
Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If uMsg = WM_HOTKEY Then
        If wParam = MY_HOTKEY Then
            MsgBox "You pressed print screen"
        End If
    End If
    WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
End Function
and on your form...
Code:
Option Explicit
Private Sub Form_Load()
    RegisterHotKey hwnd, MY_HOTKEY, 0, vbKeySnapshot
    lpPrevWndProc = SetWindowLong(hwnd, -4, AddressOf WindowProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
    UnregisterHotKey hwnd, MY_HOTKEY
End Sub
This way actually traps the key press using WM_HOTKEY which means users can't simply move the focus to a different application and take a screenshot to get the print screen of your application.


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top