0

I have a dashboard that goes full screen. Looking to make it more of a "Kiosk" view that users can't easily bypass to get to the desktop. More for routine tamper prevention. I have a button on top right for a menu that I want to keep enabled and allows a pin to be entered to close the app.

Is there a function call or library I should look into to block user activity or would I just set all controls to enable=false, etc? but they can still hit the Windows or Ctrl+Alt+Delete which I would like to prevent also.

Zippy
  • 455
  • 1
  • 11
  • 25
  • Windows has a built-in "kiosk mode". See https://learn.microsoft.com/en-us/windows/configuration/kiosk-single-app –  Apr 07 '21 at 17:50
  • IIRC you can't bypass C-A-D in your application because the OS grabs it before anything else. There may be a group policy setting you can use, I don't recall. – Duston Apr 07 '21 at 18:17
  • Correct, you cannot bypass C-A-D, not even in Kiosk mode. –  Apr 07 '21 at 19:00

1 Answers1

0

Create a FormClosing property and write the following code to disable the user closing the form.

private void FormName_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = (e.CloseReason == CloseReason.UserClosing);
        }

I also have the following code (from here) if you want to go a step further and disable the function keys example. ctrl+shift+esc (opens task manager) etc.

Put this into the FormName_Load

ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
            objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
            ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);

Then put this under public partial class FormName : Form

/* Code to Disable WinKey, Alt+Tab, Ctrl+Shift+Esc Starts Here */

        // Structure contain information about low-level keyboard input event 
        [StructLayout(LayoutKind.Sequential)]
        private struct KBDLLHOOKSTRUCT
        {
            public Keys key;
            public int scanCode;
            public int flags;
            public int time;
            public IntPtr extra;
        }
        //System level functions to be used for hook and unhook keyboard input  
        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool UnhookWindowsHookEx(IntPtr hook);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string name);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern short GetAsyncKeyState(Keys key);
        //Declaring Global objects     
        private IntPtr ptrHook;
        private LowLevelKeyboardProc objKeyboardProcess;

        private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
        {
            if (nCode >= 0)
            {
                KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));

                // Disabling Windows keys 

                if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (ModifierKeys & Keys.Control) == Keys.Control)
                {
                    return (IntPtr)1; // if 0 is returned then All the above keys will be enabled
                }
            }
            return CallNextHookEx(ptrHook, nCode, wp, lp);
        }

        bool HasAltModifier(int flags)
        {
            return (flags & 0x20) == 0x20;
        }

        /* Code to Disable WinKey, Alt+Tab, Ctrl+Shift+Esc Ends Here */

Make sure you are also using these

using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;

System.Diagnostics; and System.Runtime.InteropServices; to be specific.

With these changes I recommend making an "emergency escape" in case you want to exit.

You could also put the app into startup for it to start once the machine is logged on. code for this:

if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup).Trim() + "\\FILE.exe") == false)
                {
                    File.Move(Application.StartupPath + "\\FILE.exe", Environment.GetFolderPath(Environment.SpecialFolder.Startup).Trim() + "\\FILE.exe");
                }

I suggest making this a try catch statement if an exception is thrown for the file already being inside startup

SimpleCoder
  • 474
  • 4
  • 18
  • 2
    You should link to [where you got that code from](https://stackoverflow.com/a/3227562/47589). Always give proper attribution. –  Apr 08 '21 at 00:22
  • See the [license](https://stackoverflow.com/help/licensing) –  Apr 08 '21 at 00:29
  • @Amy I couldn't remember where it was from had it in one of my projects – SimpleCoder Apr 08 '21 at 13:52
  • We all have a responsibility to retain attribution for code we find online, especially when the license we acquire it under requires it, *especially* when its in code we're working on for our employers. That isn't an excuse. It's easy to add a comment saying "this code came from..." –  Apr 08 '21 at 13:55
  • @Amy Yes I'm sorry I just had it lying around as I don't tend to comment near the code but I comment in a special section of the app or make a readme to thank and mention the code creator/source. I have just found my file for the project. – SimpleCoder Apr 08 '21 at 13:59
  • Well, I would suggest making a change to how you do that so this sort of thing doesn't repeat itself. –  Apr 08 '21 at 14:05
  • @Amy Yes, I have went into my project file and added a comment stating where the code is from. I will continue doing this to other projects in the future etc. – SimpleCoder Apr 08 '21 at 14:07