PDA

View Full Version : سوال: کار با تابع GetAsyncKeyState در سی شارپ...



tigervb6
چهارشنبه 03 مهر 1392, 10:05 صبح
سلام دوستان
کسی هست که طرز کار تابع GetAsyncKeyState رو در سی شارپ بدونه
اگه راهنمایی کنید ممنون می شم
:متفکر:
:متفکر:

مهرداد صفا
چهارشنبه 03 مهر 1392, 11:29 صبح
با سلام.


The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.
Return Value
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
The return value is zero for the following cases:
• The current desktop is not the active desktop
• The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.;
Windows 95 does not support the left- and right-distinguishing constants


حالت کلیدی را که به عنوان پارامتر ارسال می شود را در زمان فراخوانی و یا بعد از آخرین فراخوانی مشخص می کند. با این تابع می توانید چک کنید که آیا کلیدی (در محدوده global یا کل desktop) فشرده شده است یا نه. البته مقدار برگشتی ممکن است در مورد برنامه هایی که دسترسی به آنها اجازه داده نشده (مثل برنامه های امنیتی) و یا برنامه هایی که روی یک desktop دیگر اجرا می شوند (مثل زمانی که برنامه شما user باشد و برنامه فعال Run as admin شده باشد) دقیق نباشد.
اگر مقدار برگشتی برابر با 1 باشد، کلید بعد از فراخوانی اخیر تابع تا کنون فشرده شده و اگر مقدار برگشتی برابر با 2^15 باشد، کلید در حال حاضر فشرده یا down است.
البته روی مورد اول حساب باز نکنید چرا که ممکن است مثلا کلیدی فشرده شده باشد و برنامه دیگری تابع را فراخوانی کرده باشد.
شما می توانید چک کنید که مثلا کلید alt چپ یا راست فشرده شده (البته در مورد win 95 صدق نمی کند).
مثال:

......
[System.Runtime.InteropServices.DllImport("user32")]
static extern UInt16 GetAsyncKeyState(Keys key);
...
if ((GetAsyncKeyState(Keys.A) & 0X8000) == 0X8000)
MessageBox.Show("the 'A' key is down now!");
if ((GetAsyncKeyState(Keys.B ) & 0x1) ==0X1)
MessageBox.Show("the 'B' key has pressed affter last function call");