Click here to Skip to main content
15,887,267 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to clone a videoPlayer trackbar? Pin
_Q12_1-Jan-19 8:39
_Q12_1-Jan-19 8:39 
GeneralRe: How to clone a videoPlayer trackbar? Pin
OriginalGriff1-Jan-19 19:53
mveOriginalGriff1-Jan-19 19:53 
QuestionUsing a WSDL provided by a third party in C# project Pin
Eagle3230-Dec-18 23:54
Eagle3230-Dec-18 23:54 
GeneralRe: Using a WSDL provided by a third party in C# project Pin
Richard MacCutchan31-Dec-18 3:07
mveRichard MacCutchan31-Dec-18 3:07 
GeneralRe: Using a WSDL provided by a third party in C# project Pin
Eagle3231-Dec-18 3:15
Eagle3231-Dec-18 3:15 
GeneralRe: Using a WSDL provided by a third party in C# project Pin
OriginalGriff31-Dec-18 3:29
mveOriginalGriff31-Dec-18 3:29 
GeneralRe: Using a WSDL provided by a third party in C# project Pin
OriginalGriff31-Dec-18 3:32
mveOriginalGriff31-Dec-18 3:32 
Questionc# hook - counting keystrokes on the keyboard Pin
Member 1097549730-Dec-18 5:09
Member 1097549730-Dec-18 5:09 
Statistical program. Not a keyllogger. For me, to know how much of my life I waste by tapping without meaning into the keyboard;}}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StatisticChar
{
    public partial class Form1 : Form
    {
        private GlobalKeyboardHook _globalKeyboardHook;

        public void SetupKeyboardHooks()
        {
            _globalKeyboardHook = new GlobalKeyboardHook();
            _globalKeyboardHook.KeyboardPressed += OnKeyPressed;
        }
        private void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e)
        {
            

            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown)
            {
                var znak = $"{(char)e.KeyboardData.VirtualCode} {e.KeyboardData.VirtualCode}";


                //////////////////////    SEKCJA F1   ///////////////////////////////////////////
                if ((e.KeyboardData.VirtualCode >= 112) && (e.KeyboardData.VirtualCode <= 123))
                {
                    znak = "F" + (e.KeyboardData.VirtualCode - 111).ToString();
                }

                //////////////////////   SEKCJA NUMERYCZNA ///////////////////////////////////////////

                if ((e.KeyboardData.VirtualCode >= 96) && (e.KeyboardData.VirtualCode <= 105))
                {
                    znak = (e.KeyboardData.VirtualCode - 96).ToString();
                }


                if (e.KeyboardData.VirtualCode == 144)
                {
                    znak = "NumLock";
                }

                if (e.KeyboardData.VirtualCode == 111)
                {
                    znak = "/";
                }

                if (e.KeyboardData.VirtualCode == 106)
                {
                    znak = "*";
                }

                if (e.KeyboardData.VirtualCode == 109)
                {
                    znak = "-";
                }

                if (e.KeyboardData.VirtualCode == 107)
                {
                    znak = "+";
                }

                if (e.KeyboardData.VirtualCode == 107)
                {
                    znak = ",";
                }

                //////////////////////   SEKCJA GŁÓWNA ///////////////////////////////////////////

                if (e.KeyboardData.VirtualCode == 13)
                {
                    znak = "ENTER";
                }

                if (e.KeyboardData.VirtualCode == 192)
                {
                    znak = "`";
                }

                if (e.KeyboardData.VirtualCode == 9)
                {
                    znak = "tabulator";
                }

                if (e.KeyboardData.VirtualCode == 20)
                {
                    znak = "capslock";
                }

                if (e.KeyboardData.VirtualCode == 160)
                {
                    znak = "LewyShift";
                }

                if (e.KeyboardData.VirtualCode == 162)
                {
                    znak = "ctrl";
                }

                if (e.KeyboardData.VirtualCode == 91)
                {
                    znak = "win";
                }

                if (e.KeyboardData.VirtualCode == 32)
                {
                    znak = "spacja";
                }

                if (e.KeyboardData.VirtualCode == 37)
                {
                    znak = "←";
                }

                if (e.KeyboardData.VirtualCode == 39)
                {
                    znak = "→";
                }

                if (e.KeyboardData.VirtualCode == 38)
                {
                    znak = "↑";
                }

                if (e.KeyboardData.VirtualCode == 40)
                {
                    znak = "↓";
                }

                if (e.KeyboardData.VirtualCode == 8)
                {
                    znak = "backup";
                }

                if (e.KeyboardData.VirtualCode == 161)
                {
                    znak = "prawyShift";
                }

                if (e.KeyboardData.VirtualCode == 93)
                {
                    znak = "ppm";
                }

                if (e.KeyboardData.VirtualCode == 45)
                {
                    znak = "insert";
                }

                if (e.KeyboardData.VirtualCode == 44)
                {
                    znak = "printscreen";
                }

                if (e.KeyboardData.VirtualCode == 19)
                {
                    znak = "pause";
                }

                if (e.KeyboardData.VirtualCode == 166)
                {
                    znak = "back";
                }

                if (e.KeyboardData.VirtualCode == 36)
                {
                    znak = "home";
                }

                if (e.KeyboardData.VirtualCode == 35)
                {
                    znak = "end";
                }

                if (e.KeyboardData.VirtualCode == 46)
                {
                    znak = "delete";
                }

                if (e.KeyboardData.VirtualCode == 33)
                {
                    znak = "pageUp";
                }

                if (e.KeyboardData.VirtualCode == 34)
                {
                    znak = "pagDown";
                }

                if (e.KeyboardData.VirtualCode == 163)
                {
                    znak = "PrawyCtrl";
                }



                //////////////////////   INNE, POZOSTAŁE ///////////////////////////////////////////

                if (e.KeyboardData.VirtualCode == 1)
                {
                    znak = "LeftClick";
                }

                if (e.KeyboardData.VirtualCode == 2)
                {
                    znak = "RightClick";
                }



                Statystyki[znak] = Statystyki.ContainsKey(znak) ? ++Statystyki[znak] : 1;
            }
        }
        public new void Dispose()
        {
            base.Dispose();
            _globalKeyboardHook?.Dispose();
        }

       
        private DateTime data_zbierania_statystyk;
        private static readonly Dictionary<string, int> Statystyki = new Dictionary<string, int>();

        public Form1()
        {
            _globalKeyboardHook = new GlobalKeyboardHook();
            SetupKeyboardHooks();

            var dt = DateTime.Now;
            data_zbierania_statystyk = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);

            Timer timer = new Timer
            {
                Interval = 1 * 10 * 1000
            };
            timer.Tick += new EventHandler(Timer_Tick);
            timer.Start();

            Timer timer2 = new Timer
            {
                Interval = 1000
            };
            timer2.Tick += new EventHandler(Timer2_Tick);
            timer2.Start();

            InitializeComponent();

        }

        private void Timer2_Tick(object sender, EventArgs e)
        {
           richTextBox1.Text = StatystykiJakoText();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            Saves();
        }
        void Saves()
        {
            var dt = DateTime.Now;
            var now = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);

            if (data_zbierania_statystyk < now)
            {
                data_zbierania_statystyk = now;

                string sb = StatystykiJakoText();

                string path = $@"O:\statystyki{dt.Year}-{dt.Month}-{dt.Day}.txt";

                File.AppendAllText(path, sb);

                richTextBox1.Text = "";
                richTextBox1.Text = sb.ToString();
            }
        }

        private string StatystykiJakoText()
        {
            var sb = new StringBuilder();
            sb.Append(Environment.NewLine);
            sb.Append($"Statystyki za dzień: {data_zbierania_statystyk}{Environment.NewLine}");

            foreach (var literka in Statystyki)
            {
                sb.Append($"{literka.Key} - {literka.Value}{Environment.NewLine}");
            }

            return sb.ToString();
        }

       
    }
}


However, there is still a problem:

1. I do not catch every character, eg right alt, F Mode
2. Does not catch characters written in some programs, eg Notepad ++?
Is anyone able to say why this is happening and how to fix it?

3. How to handle shortcuts eg ctrl + c, ctrl + v?
4. How to protect against pressing the key, if we keep it and do not let it go?

modified 30-Dec-18 11:16am.

AnswerRe: c# hook - counting keystrokes on the keyboard Pin
OriginalGriff30-Dec-18 5:13
mveOriginalGriff30-Dec-18 5:13 
GeneralRe: c# hook - counting keystrokes on the keyboard Pin
Member 1097549730-Dec-18 5:14
Member 1097549730-Dec-18 5:14 
JokeRe: c# hook - counting keystrokes on the keyboard Pin
Luc Pattyn30-Dec-18 5:14
sitebuilderLuc Pattyn30-Dec-18 5:14 
GeneralRe: c# hook - counting keystrokes on the keyboard Pin
Member 1097549730-Dec-18 5:18
Member 1097549730-Dec-18 5:18 
AnswerRe: c# hook - counting keystrokes on the keyboard Pin
Gerry Schmitz30-Dec-18 8:40
mveGerry Schmitz30-Dec-18 8:40 
AnswerRe: c# hook - counting keystrokes on the keyboard Pin
Eddy Vluggen30-Dec-18 23:15
professionalEddy Vluggen30-Dec-18 23:15 
QuestionDgv vertically its possible? Pin
Member 1097549730-Dec-18 4:43
Member 1097549730-Dec-18 4:43 
AnswerRe: Dgv vertically its possible? Pin
OriginalGriff30-Dec-18 4:55
mveOriginalGriff30-Dec-18 4:55 
GeneralRe: Dgv vertically its possible? Pin
Member 1097549730-Dec-18 5:11
Member 1097549730-Dec-18 5:11 
GeneralRe: Dgv vertically its possible? Pin
OriginalGriff30-Dec-18 5:17
mveOriginalGriff30-Dec-18 5:17 
QuestionSearch file remotely C# Pin
Member 1410307028-Dec-18 21:55
Member 1410307028-Dec-18 21:55 
AnswerRe: Search file remotely C# Pin
pkfox28-Dec-18 22:33
professionalpkfox28-Dec-18 22:33 
GeneralRe: Search file remotely C# Pin
#realJSOP30-Dec-18 2:53
mve#realJSOP30-Dec-18 2:53 
AnswerRe: Search file remotely C# Pin
BillWoodruff29-Dec-18 1:05
professionalBillWoodruff29-Dec-18 1:05 
AnswerRe: Search file remotely C# Pin
#realJSOP30-Dec-18 2:56
mve#realJSOP30-Dec-18 2:56 
AnswerRe: Search file remotely C# Pin
Gerry Schmitz30-Dec-18 8:28
mveGerry Schmitz30-Dec-18 8:28 
QuestionUsing A FileStream As A Texture? Pin
SawmillTurtle26-Dec-18 17:54
SawmillTurtle26-Dec-18 17:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.