C # keyboard monitoring + clipboard programming (each copy is different! * V *)

Source: Internet
Author: User

Origin:
I uploaded all my favorite Google logos to my blog some time ago. I found that uploading images in batches is not supported, and the names of uploaded images are different !! Rely on me! More than one hundred images can only be uploaded after the year and month of the monkey !! So I tried to get lazy: First I got a tool to change the file name in batches and changed the image to 1 ~ I wrote a clipboard program to dynamically change the file name copied each time. In this way, I only need to press Ctrl + C each time, Click Upload, and then press Ctrl + C to upload the file...
Implementation:
First, I wrote all the code in one brain, and then explained it a little bit:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;
Using system. Windows. forms;
Using system. diagnostics;

Namespace clipboardedit
{
Public class interceptkeys
{
Private const int wh_keyboard_ll = 13;

Private const int wm_keydown = 0x0100;

Private Static lowlevelkeyboardproc _ proc = hookcallback;

Private Static intptr _ hookid = intptr. zero;

/// <Summary>
/// Start the Monitoring Program
/// </Summary>
Public static void runhook ()
{
_ Hookid = sethook (_ proc );
}

/// <Summary>
/// Close the Monitoring Program
/// </Summary>
Public static void unhook ()
{
Unhookwindowshookex (_ hookid );
}

Private Static intptr sethook (lowlevelkeyboardproc proc)
{
Using (process curprocess = process. getcurrentprocess ())
Using (processmodule curmodule = curprocess. mainmodule)
{
Return setwindowshookex (wh_keyboard_ll, Proc, getmodulehandle (curmodule. modulename), 0 );
}
}

/// <Summary>
/// Set the default value and Data
/// </Summary>
/// <Param name = "initint"> </param>
/// <Param name = "outstring"> "F: \ image \ boutique category \ brand image \ Google logo \@_@. GIF" </param>
Public static void Init (INT initint, string outstring)
{
I = initint;
STR = outstring;
}

Private Static int I = 0;
Private Static string STR = @ "I: \ over \ images \ google \ @ _ @. jpg ";

Private Static intptr hookcallback (INT ncode, intptr wparam, intptr lparam)
{
If (ncode> = 0 & wparam = (intptr) wm_keydown)
{
Int vkcode = marshal. readint32 (lparam );
Keys key = (KEYS) vkcode;
// Press Ctrl + V at the same time
If (control. modifierkeys = keys. Control & Key. Equals (Keys. V ))
{
// Obtain the Clipboard data
String data = clipboard. gettext (textdataformat. unicodetext );
// Reset the Clipboard data
Dataobject m_data = new dataobject ();
M_data.setdata (dataformats. Text, true, str. Replace ("@ _ @", convert. tostring (++ I )));
Clipboard. setdataobject (m_data, true );
}
}
Return callnexthookex (_ hookid, ncode, wparam, lparam );
}

# Region call API

Private delegate intptr lowlevelkeyboardproc (INT ncode, intptr wparam, intptr lparam );

[Dllimport ("user32.dll", charset = charset. Auto, setlasterror = true)]

Private Static extern intptr setwindowshookex (INT idhook,

Lowlevelkeyboardproc lpfn, intptr hmod, uint dwthreadid );

[Dllimport ("user32.dll", charset = charset. Auto, setlasterror = true)]

[Return: financialas (unmanagedtype. bool)]

Private Static extern bool unhookwindowshookex (intptr HHK );

[Dllimport ("user32.dll", charset = charset. Auto, setlasterror = true)]

Private Static extern intptr callnexthookex (intptr HHK, int ncode,

Intptr wparam, intptr lparam );

[Dllimport ("kernel32.dll", charset = charset. Auto, setlasterror = true)]

Private Static extern intptr getmodulehandle (string lpmodulename );

# Endregion
}
}

Call:

Interceptkeys. runhook ();

Note:
In fact, there is not so much code for displaying or changing the data in the clipboard. Here, the keyboard monitoring program is started. Only pressing CTRL + C will change the data in the clipboard, like [dllimport ("kernel32.dll", charset = charset. auto, setlasterror = true)] DLL is used to call the system API to monitor Keyboard Events.

Conclusion:
Apply what you have learned to your life so that you can learn things and solve practical problems while learning things. I couldn't use half of the time to write this code to upload all in the original stupid way, But you know-I earned it !! :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.