Thursday, August 5, 2010

Emulate Keystrokes in C# (Never Rest Application)



This code segment is much like my last entry. This uses the SendKeys class, and emulates keystrokes typed by users in C#.  I wrote a program called Never Rest that prevents the screensaver or timeouts from happening by emulating keystrokes. The command it runs is the F16 (non existent) key command.  Please view the code segment below. I have also included a screenshot of my application I had written, and in lo and behold around 100 lines of code.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace NeverRest
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }


        string appTitle = "Never Rest";
        private void frmMain_Load
            (object sender, 
            EventArgs e)
        {
            this.Visible = false;
            ntyNR.ShowBalloonTip(5000,
                                   appTitle,
                                   "Welcome to Never Rest!",
                                    ToolTipIcon.Info);
            
            tmrNR.Interval = 
                60 * Convert.ToInt32
                (nmrMin.Value) * 
                1000;
        }


        private void nmrMin_ValueChanged
            (object sender, 
            EventArgs e)
        {
            tmrNR.Interval =
                60 * Convert.ToInt32
                (nmrMin.Value) * 1000;
        }
        
        private void toolStripSeparator1_Click
            (object sender, 
            EventArgs e)
        {
            ExitMe();
        }
        private void ExitMe()
        {
            if (MessageBox.Show(
"Are you sure you want to exit? Click yes to confirm and no to continue",
                appTitle + ": Exit",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question) ==
                DialogResult.Yes)
            {
                Application.Exit();
            }
            else
            {


            }
        }


        private void 
            exitApplicationToolStripMenuItem_Click
            (object sender, EventArgs e)
        {
            this.ExitMe();
        }


        private void button2_Click(object sender, 
            EventArgs e)
        {
            this.HideShow();
        }
        void HideShow()
        {
            if (this.Visible == true)
            {
                this.BringToFront();
                this.Hide();
                
            }
            else
            {
                this.BringToFront();
                this.WindowState = 
                    FormWindowState.Normal;
                this.Show();
            }
        }


        private void hideShowToolStripMenuItem_Click
            (object sender, EventArgs e)
        {
            this.HideShow();
        }


        private void button1_Click
            (object sender, EventArgs e)
        {
            this.ExitMe();
        }


        private void ntyNR_MouseDoubleClick
            (object sender, MouseEventArgs e)
        {
            this.HideShow();
        }


        private void tmrNR_Tick
            (object sender, EventArgs e)
        {
            SendKeys.Send("{F16}");
        }


    }
}

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...