/*

CLASS {Current_input_lang|uage_CSharp}, UNTESTED, NATIVE:

IMPORT: {Keyboard} .

NATIVE: "///COPY Current_input_lang.cs" .

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
//using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using AL4;

namespace AL4_project
{

	public class AL4_keyboardlayout
	{
	
		[DllImport("user32.dll", SetLastError = true)]
		static extern uint GetKeyboardLayout([In] int idThread);
		
		[DllImport("user32.dll", SetLastError = true)]
		static extern int GetWindowThreadProcessId(
		[In] IntPtr hWnd,[Out, Optional] IntPtr lpdwProcessId);
		
		[DllImport("user32.dll", SetLastError = true)]
		static extern IntPtr GetForegroundWindow();
		
		public static uint Get_keyboard_layout()
		{
		    return GetKeyboardLayout(
		    		GetWindowThreadProcessId(
		    			GetForegroundWindow(), IntPtr.Zero));
		}
		
		///////////////////////////////////////////////////////
		
        //[DllImport("user32.dll", SetLastError = true)]
        //[return: MarshalAs(UnmanagedType.Bool)]
        //static extern bool GetKeyboardState(byte [] lpkeystate);
		
        //[DllImport("user32.dll")]
        //static extern short GetAsyncKeyState(int key);
		
		[DllImport("user32.dll")]
        static extern short GetKeyState(int key);
		
		[DllImport("user32.dll")]
		static extern void keybd_event(byte bvk, byte bscan, uint dwFlags,
			int dwExtraInfo);
		
		public static void Set_scroll_lock(bool on)
		{
			//byte[] keystate = new byte[256];
			//GetKeyboardState(keystate);
			//if( (on && !(keystate[VK_SCROLL] & 1))
			bool was_on = (GetKeyState(0x91) & 1) != 0;
			if(on != was_on)
			{
				//keybd_event(VK_SCROLL, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
				keybd_event(0x91, 0x46, 1 | 0, 0);
				//keybd_event(VK_SCROLL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
				keybd_event(0x91, 0x46, 1 | 2, 0);
			}
		}
		
	}
	
}

/*

FUNCTION Get|_current_language ==> INT,
NATIVE : @
"RESULT = AL4_keyboardlayout.Get_keyboard_layout();" .

FUNCTION Set_scroll_lock(BOOL Light_on), NATIVE : @
    "AL4_keyboardlayout.Set_scroll_lock(X_Light_on);" 
    "AL4_keyboardlayout.Set_scroll_lock(X_Light_on);" 
    .

END

*/
       
