Przesłuchiwanie bufora

0

Napisałem taki oto prosty programik:

#include <Windows.h>
#include <iostream>


using namespace std;
 
const int baseOfSigns=255;

char interrogateBuffer()
{
	int i=0;
	
	for (i=baseOfSigns; i>0 ; i--)
			for (i=0; i<baseOfSigns;i++)
			{	
				if (GetAsyncKeyState(i))
				return 	static_cast<char>(i);
			}		
}

int main(void)
{
	
	while(interrogateBuffer() != '`')
	cout<<interrogateBuffer();
	
    system("PAUSE");
}

Jak pewnie większość starych wyjadaczy się domyśliła problemy są dwa:

  1. Po nieważne jak króciutkim stuknięciu klawisza w konsoli pojawia się kilkaset jego kopii. Jak odpowiednio "spowolnić" program aby wczytywana była naturalna liczba znaków(nie chcę stosować opóźnienia na "chybił trafił" tak jak to chyba jest tutaj Prosty i skuteczny KeyLogger dla Windows) ?

  2. Dlaczego zawsze wyświetlają się wielkie litery?

0

może, zapisz sobie czy klawisz w poprzedniej iteracji był wciśnięty czy nie i reaguj tylko na zmiany...

0
krwq napisał(a)

może, zapisz sobie czy klawisz w poprzedniej iteracji był wciśnięty czy nie i reaguj tylko na zmiany...

Mógłbym prosić o fragment kodu ? Jakoś nie potrafię wprowadzić tej koncepcji.

0

SHORT WINAPI GetAsyncKeyState(
__in int vKey
);

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks.

http://msdn.microsoft.com/en-us/library/ms646293

0
bool byl_wcisniety = GetAsyncKeyState(VK_LSHIFT);
 
while(...) {
   bool jest_wcisniety = GetAsyncKeyState(VK_LSHIFT);
   if (byl_wcisniety != jest_wcisniety) cout << jest_wcisniety ? "Wcisnąłeś lewy shift " : "Zwolniles lewy shift ";
   byl_wcisniety = jest_wcisniety;
}

źródło : http://4programmers.net/Forum/C_i_C++/193154-runtime_error-_zmienna_niezadeklarowana
Skoro da się zastosować dla pojedyńczego klawisza, to musi się dać i dla wszystkich, z tą różnicą że chyba zmienne byl_wcisniety i jest_wcisniety muszą być tablicowe...

1
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace ConsoleApplication8
{
    class Program
    {
        [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
        public static extern short GetAsyncKeyState(int vkey);

        static short sh1;
        static short sh2;
        byte[] bAray = new byte[2];
        static byte B1;
        static byte B2;
        static byte B3;
        static byte B4;

        static void FromShort(short number, out byte byte1, out byte byte2)
        {
            byte2 = (byte)(number >> 8);
            byte1 = (byte)(number & 255);
        }

        static void Main(string[] args)
        {

            for (;;)
            {

                sh1 = GetAsyncKeyState(0X10);
                sh2 = GetAsyncKeyState(0x41);
                FromShort(sh1, out B1, out B2);
                FromShort(sh2, out B3, out B4);
                if ((B4 == 128 && B2 == 128) && (B3 == 1 || B1 == 1))
                {

                    Console.WriteLine("WCISNALES SHIFT + A");

                }

            }
        }
    }
}
0

Nie da się jakoś w C++ ?

1

No jasne, że się da. OMG.

 
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>


int short sh1;
int short sh2;

int short byte1;
int short byte2;
int short byte3;
int short byte4;

void FromShort(int short number, int short &byte1, int short &byte2);

int _tmain(int argc, _TCHAR* argv[])
{

    for (;;)
    {

        sh1 = GetAsyncKeyState(0X10);
        sh2 = GetAsyncKeyState(0x41);
        FromShort(sh1, byte1, byte2);
        FromShort(sh2, byte3, byte4);

        if ((byte4 == -128 && byte2 == -128) && (byte3 == 1 || byte1 == 1))
        {

			std:: cout << "WCISNALES SHIFT + A" << std::endl;

        }

    }

	return 0;
}

void FromShort(int short number, int short &byte1, int short &byte2)
{

	byte2 = (number >> 8);
    byte1 = (number & 255);

}

1 użytkowników online, w tym zalogowanych: 0, gości: 1