AutoSaver

October 27th, 2009 Leave a comment Go to comments

Downloads:

Executable
Source Code (C#)

Description:

Auto Saver is an application that will send a user defined key stroke to a designated application through the user32.dll api based on a timer,
the numlock key is used to suspend sending of the key sequence. If either the numlock key is off, or the user defined application is not running,the application will do nothing.

The application was designed to introduce a autosave feature to EA’s spore video game, and thus its name was derived.

The key sequences used follow this format.
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

Screen Shot(s):

autosaverscreen

Notable Code:

Code used to make sure desired process is running, set the process to forground, and send key sequence

        static public IntPtr getWin(string process_name)
        {
            Process[] processes = Process.GetProcessesByName(process_name);
            IntPtr win_handle = new IntPtr();
            foreach (Process p in processes)
            {
                win_handle = p.MainWindowHandle;
            }
            return win_handle;
        }

        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
         

        private void saver_fire(object sender, EventArgs e)
        {
            //user only needs processname, not class and handle
            string processName = Properties.Settings.Default.txtProcess;
            IntPtr applicationHandle = getWin(processName);

            // Verify that application is a running process.
            if (applicationHandle != IntPtr.Zero && Console.NumberLock)
            {
                // Make application the foreground application and send it 
                // the keys you want.
                SetForegroundWindow(applicationHandle);
                string keySeq = Properties.Settings.Default.txtSequence.ToString();
                SendKeys.SendWait(keySeq);
                //MessageBox.Show("numlock is on, and window has focus");
            }
            else
            {
                //MessageBox.Show("numlock is off, or window doesn't have focus");
                return;
            }
        }

  1. No comments yet.
  1. No trackbacks yet.