20 Aralık 2013 Cuma

C# da Tuş Okuma

using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace projeadi
{
public static class ReadKey
{
#region events
public static delegate void _GetKey(Keys key,int ascii,bool down);
public static event _GetKey GetKey;
#endregion
#region api
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(long vKey);
#endregion
#region variables
public static bool[] keys = new down[255];
internal static Thread _readmethods;
internal static bool load=false;
#endregion
#region methods
public static bool Load()
{
if(load)
return false;
load=true;
_readmethods = new Thread(readkeys);
_readmethods.Start();

return true;
}

internal static bool down(int ascii)
{
return (GetAsyncKeyState(ascii) == -32767 || GetAsyncKeyState(ascii) == -32768);
}
internal static void readkeys()
{
while(true)
{
for(int i = 1;i <= 255;i++)
{
#region down
if(down(i))
{
if(!keys[i])
{
keys[i]=true;
if(GetKey!=null)
GetKey((Keys)i,i,keys[i]);
}
}
#endregion
#region up
else
{
if(keys[i])
{
keys[i]=false;
if(GetKey!=null)
GetKey((Keys)i,i,keys[i]);
}
}
#endregion
}
Thread.Sleep(1);
}
}
#endregion
}
}

0 yorum :

Yorum Gönder