Quick update on ‘Hijinks at work’

Okay, so here’s an update on the Hijinks at work post.

The mark, who didn’t lock his computer yesterday, came into work this morning and started up his computer. I had the MMR app set in his startup folder, so when he logged in, the mouse started jumping every few seconds in a random direction. All of us in the room resisted the urge to laugh maniacally.

But then we noticed that he hadn’t tried to fix the problem yet. He acted oblivious to it. Only after about twenty minutes did he finally start to try and solve it. Now, the first thing I would have done was check for a remote session in VNC, and then the task manager for a rogue program. The mark, however, went to the control panel, and starting with the mouse setting, attempted to ‘fix it’.

At this point, the rest of us started sending emails to each other, since only I had line-of-sight to this guy’s monitor, so I could keep them informed. I watched as he searched through settings windows trying to solve it. He even picked up his mouse and shook it.

And then he shut down his computer, and left to go do other work.

Mission accomplished! And even better, since he didn’t solve it, it’ll happen again when he gets back.

Hijinxs at work

We have a co-worker who doesn’t understand that he’s got to lock his computer when he steps away from it, so we all decided to play a prank on him. Of course, there’s the standard “tape over laser under mouse” maneuver. Then there’s the “change his desktop backgrounds” or “change his Windows sounds” tactic. Mine was a bit more devious.

Below is the source code for a tiny program to run in the background, detectable only through task manager, that randomly moves the mouse pointer.

(Note: I use the .settings file because I can change the settings later to fine-tune them)

And here’s the Program.cs code. Create a Windows Form application (this is the important part – don’t create a Console app!), then delete the form and replace the Program.cs code with the code below. Compile and enjoy!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace mmr_e12
{
    /// <summary>
    /// Move Mouse Randomly Event 2012 Application
    /// </summary>
    static class Program
    {
        #region Settings

        /// <summary>
        /// The update speed for the Timer
        /// </summary>
        static int _MaxInterval = 2000;

        /// <summary>
        /// The maximum distance from the initial mouse position
        /// </summary>
        static int _MouseRadius = 100;

        /// <summary>
        /// Whether or not to use smooth mouse transitions
        /// </summary>
        static bool _UseSmoothness = true;

        /// <summary>
        /// The duration in seconds for the smooth mouse transition
        /// </summary>
        static double _SmoothnessDuration = 0.1;

        /// <summary>
        /// The perceived speed of the smooth transition
        /// (if Frame Time = 10ms, Perceived Speed = 100.0)
        /// </summary>
        static double _PerceivedSpeed = 500.0;

        #endregion Settings
        
        //**********************************



        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            try {
                // Create a Random Number Generator
                Random r = new Random();

                // Create a Windows Form Timer
                System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

                // Set the initial Interval low to get it going.
                timer.Interval = 100;

                // Set the Tick Event to update the mouse position
                timer.Tick += (sender, args) => {
                    
                    // Randomly determine change in position
                    var dx = r.Next(0, _MouseRadius * 2) - _MouseRadius;
                    var dy = r.Next(0, _MouseRadius * 2) - _MouseRadius;
                    
                    // Randomly determine next update interval
                    var z = (int)(_MaxInterval * r.NextDouble());
                    
                    // Prevent zero for update interval
                    if (z <= 0) z = 1;

                    // Move the cursor
                    if (_UseSmoothness)
                        SmoothMouseMove(dx, dy, _SmoothnessDuration);
                    else
                        Cursor.Position = new Point(Cursor.Position.X + dx, Cursor.Position.Y + dy);

                    // Update the Interval to increase the randomness
                    timer.Interval = z;
                };

                // Start the Timer
                timer.Start();

                // Run the Application Thread
                Application.Run();
            }
            catch (Exception Error) {
                // Show Error - no fun if failed!
                MessageBox.Show(string.Format("{0}", Error), "Application Error");
                
                // Exit application thread.
                Application.Exit();
            }
        }

        /// <summary>
        /// Smoothly transition the mouse pointer between it's current position and it's new position
        /// </summary>
        /// <param name="dx">Change in X Position</param>
        /// <param name="dy">Change in Y Position</param>
        /// <param name="durationInSeconds">The length of the transition time.</param>
        static void SmoothMouseMove(int dx, int dy, double durationInSeconds) {
            
            // Check for divide-by-zero & transition time
            if (!(durationInSeconds > 0))
                throw new ArgumentException("Transition Duration must be greater than 0.", "durationInSeconds");

            // Number of frames to "render"
            double frames = durationInSeconds * _PerceivedSpeed;

            // Length of pause time between frames
            int frameSleepLength = (int)((durationInSeconds / frames) * _PerceivedSpeed);

            // Create the change vector
            PointF vector = new PointF((float)(dx / frames), (float)(dy / frames));
            
            // Get the initial mouse position
            PointF mousePos = Cursor.Position;

            // Move through each frame of animation
            for (int i = 0; i < frames; i++) {
                
                // Set the new Cursor Position
                Cursor.Position = new Point((int)(mousePos.X += vector.X), (int)(mousePos.Y += vector.Y));

                // Sleep to transition over perceived time
                Thread.Sleep(frameSleepLength);
            }

        }
    }

    
}