[System.Runtime.InteropServices.DllImport("User32.dll")] private static extern short GetAsyncKeyState(System.Int32 vKey); private void DoCheck() { foreach (...) { short keyEsc = GetAsyncKeyState((int)Keys.Escape); if (keyEsc != 0) break; ... } }