Copy Paste Question

Hello All!

So, I have a question that may be a little bit hard to explain. First off, let me give you my background so you know at what level to talk to me at haha.

I am in college for computer networking, programming routers, so I am pretty tech savvy. I also have a background in computers since I was like 10 when I got my first DOS IBM from my grandma. I know very basics of C++ and VB.net.

My question to you, how would I go about storing copy paste data in a string, depending on which key I press.

So I am going to buy a game pad, with extra keys that aren't on a standard keyboard. I am going to use, for example, Key 1 on the game pad to copy the selected text to String1, Key 2 to copy to String 2, and so on.

Then I am going to be pasting String 1 by pressing BKey1, and String 2 by BKey2, you get the idea.

What would be the best way of going about this, because it is MOSTLY going to be copying excel cells, so I don't want to lose that formatting by just copying text. I am thinking of hooking the windows copy and paste somehow, but just storing it inside of the program I create, so that when I press those keys it can be called out into whatever field I have the cursor in.

Any thoughts or ideas are greatly appreciated, I am going to be using this for a business environment, so I may be replicating it many times, something I have to keep in mind.

After a little bit more searching, I found ClipX, which is ALMOST what I need, I would like the ability to save 3 at a minimum, however.

Thanks for reading!
Last edited on
IIRC WindowsXP+ allow you to store multiple values on the clipboard from excel anyways. So this functionality is built in to the operating system.
Could you explain to me on how to unlock this function, please? It isn't going from excel to excel, it is going excel to chrome, actually, a web based application our company uses.
I wouldn't write this code in C++. I'd look at something much easier like Python/Ruby.
Hi SliverOrange,

I can suggest creating a Global Keyboard Hook using Windows API. I have created a simple Code which does that but you can edit this hook to your liking:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<Windows.h>
#include <stdio.h>

HHOOK hHock = NULL;

LRESULT CALLBACK MyKeyboard ( int nCode , WPARAM wParam , LPARAM lParam)
{
    printf("Key press\n");
    return CallNextHookEx(hHock , nCode ,wParam , lParam);
}

int main()
{
    MSG msg;
    hHock = SetWindowsHookEx(WH_KEYBOARD_LL , MyKeyboard , NULL,NULL);

    while(!GetMessage(&msg, NULL, NULL, NULL)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    UnhookWindowsHookEx(hHock);
} 
}


If you wish to learn more about hooking I would consider learning Assembly as it allows Lower Level Interaction with Ring3. Of course MSDN Libraries offers great help for Learners.
Link: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589(v=vs.85).aspx

Be very cautious about where you go and learn about hooking as it is a very sensitive subject as there are Malicious Uses which can be achieved with the help of hooking.



Hope this helps!

Kind Regards,
SpaceWorm
Last edited on
Thanks SpaceWorm! I have dabbled in assembly, but not in depth at all. Thanks for the tips and advice, much appreciated!
No problem, Helpful Information: As Hooking is mostly redirecting a function to another function you need to learn to use Sorry it is assembly
1
2
3
4
5
JMP ; Unconditional jump 
JE   ; Conditional Jump
JG   ; Conditional Jump
JB   ; Conditional Jump 
;There are plenty more Jumps but for me they helped me so far

Of course other Assembly is also very important for other programming

I have created a Code for you which does a Local Application Level hook API:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <windows.h>
#include <iostream>
#include <conio.h>
PBYTE HookFunction( PBYTE pFunc, PBYTE pHook ) //returns oFunc
{
    if( (pFunc == NULL) || (pHook == NULL) )
  return NULL;

    PBYTE oFunc = (PBYTE) VirtualAlloc(NULL, 32, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);

    DWORD oldProtect;
    VirtualProtect( (PBYTE)pFunc, 5, PAGE_EXECUTE_READWRITE, &oldProtect );

    for( unsigned int i = 0; i < 5; i ++ )
  *(BYTE *) &oFunc[i] = *(BYTE *) (pFunc + i);

    *(BYTE *) &oFunc[5] = (BYTE) 0xE9;
    *(DWORD *) &oFunc[6] = (DWORD) (pFunc - oFunc - 5);

    *(BYTE *) &pFunc[0] = (BYTE) 0xE9;
    *(DWORD *) &pFunc[1] = (DWORD) ( pHook - pFunc - 5 );
    VirtualProtect( (PBYTE)pFunc, 5, oldProtect, &oldProtect );

    return oFunc;
}

PBYTE oMsgBox;

int myMessageBoxA( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType )
{
	return ((int(*)(int, int, int, int))oMsgBox)(0,0, 0, 0);
}

int main()
{
    FARPROC pFunc = GetProcAddress( GetModuleHandle("user32.dll"), "MessageBoxA");
    oMsgBox = HookFunction( (PBYTE)pFunc, (PBYTE)myMessageBoxA );
    MessageBox(0, "Hello World!", 0, 0); /*You should not see any Text on Message Box*/
    return 0;
}


Although this uses no Assembly nor any Lower Level Inline Code it can be easily adapted to Global Level Hooks. In which case this can be used without having any direct references to any Assembly. This has only been tested on 32 bit systems so far but not sure if this could work on 64 bit systems.

I shall not give code to using assembly to hook as it would be foolish of me to give it as It took me around a month to start Hooking using Assembly.

Kind Regards
Last edited on
Topic archived. No new replies allowed.