fn load_keyboard_layout(&mut self) {
        unsafe {
            self.key_vals.clear();
            self.dead_keys.clear();
            self.has_altgr = false;
            let mut key_state = [0u8; 256];
            let mut uni_chars = [0u16; 5];
            // Right now, we're only getting the values for base and shifted
            // variants. Mozilla goes through 16 mod states.
            for shift_state in 0..N_SHIFT_STATE {
                let has_shift = shift_state & SHIFT_STATE_SHIFT != 0;
                let has_altgr = shift_state & SHIFT_STATE_ALTGR != 0;
                key_state[VK_SHIFT as usize] = if has_shift { 0x80 } else { 0 };
                key_state[VK_CONTROL as usize] = if has_altgr { 0x80 } else { 0 };
                key_state[VK_LCONTROL as usize] = if has_altgr { 0x80 } else { 0 };
                key_state[VK_MENU as usize] = if has_altgr { 0x80 } else { 0 };
                key_state[VK_RMENU as usize] = if has_altgr { 0x80 } else { 0 };
                for vk in PRINTABLE_VKS.iter().cloned().flatten() {
                    let ret = ToUnicodeEx(
                        vk as UINT,
                        0,
                        key_state.as_ptr(),
                        uni_chars.as_mut_ptr(),
                        uni_chars.len() as _,
                        0,
                        self.hkl,
                    );
                    match ret.cmp(&0) {
                        Ordering::Greater => {
                            let utf16_slice = &uni_chars[..ret as usize];
                            if let Ok(strval) = String::from_utf16(utf16_slice) {
                                self.key_vals.insert((vk, shift_state), strval);
                            }
                            // If the AltGr version of the key has a different string than
                            // the base, then the layout has AltGr. Note that Mozilla also
                            // checks dead keys for change.
                            if has_altgr
                                && !self.has_altgr
                                && self.key_vals.get(&(vk, shift_state))
                                    != self.key_vals.get(&(vk, shift_state & !SHIFT_STATE_ALTGR))
                            {
                                self.has_altgr = true;
                            }
                        }
                        Ordering::Less => {
                            // It's a dead key, press it again to reset the state.
                            self.dead_keys.insert((vk, shift_state));
                            let _ = ToUnicodeEx(
                                vk as UINT,
                                0,
                                key_state.as_ptr(),
                                uni_chars.as_mut_ptr(),
                                uni_chars.len() as _,
                                0,
                                self.hkl,
                            );
                        }
                        _ => (),
                    }
                }
            }
        }
    }
/// Load a keyboard layout.
///
/// We need to retain a map of virtual key codes in various modifier
/// states, because it's not practical to query that at keyboard event
/// time (the main culprit is that `ToUnicodeEx` is stateful).
///
/// The logic is based on Mozilla KeyboardLayout::LoadLayout but is
/// considerably simplified.
Load a keyboard layout.
We need to retain a map of virtual key codes in various modifier
states, because it's not practical to query that at keyboard event
time (the main culprit is that `ToUnicodeEx` is stateful).

The logic is based on Mozilla KeyboardLayout::LoadLayout but is
considerably simplified.
Load a keyboard layout . We need to retain a map of virtual key codes in various modifier states because it ' s not practical to query that at keyboard event time ( the main culprit is that ` ToUnicodeEx ` is stateful ) . The logic is based on Mozilla KeyboardLayout :: LoadLayout but is considerably simplified .
fn load_keyboard_layout(&mut self) {
        unsafe {
            self.key_vals.clear();
            self.dead_keys.clear();
            self.has_altgr = false;
            let mut key_state = [0u8; 256];
            let mut uni_chars = [0u16; 5];
            // Right now, we're only getting the values for base and shifted
            // variants. Mozilla goes through 16 mod states.
            for shift_state in 0..N_SHIFT_STATE {
                let has_shift = shift_state & SHIFT_STATE_SHIFT != 0;
                let has_altgr = shift_state & SHIFT_STATE_ALTGR != 0;
                key_state[VK_SHIFT as usize] = if has_shift { 0x80 } else { 0 };
                key_state[VK_CONTROL as usize] = if has_altgr { 0x80 } else { 0 };
                key_state[VK_LCONTROL as usize] = if has_altgr { 0x80 } else { 0 };
                key_state[VK_MENU as usize] = if has_altgr { 0x80 } else { 0 };
                key_state[VK_RMENU as usize] = if has_altgr { 0x80 } else { 0 };
                for vk in PRINTABLE_VKS.iter().cloned().flatten() {
                    let ret = ToUnicodeEx(
                        vk as UINT,
                        0,
                        key_state.as_ptr(),
                        uni_chars.as_mut_ptr(),
                        uni_chars.len() as _,
                        0,
                        self.hkl,
                    );
                    match ret.cmp(&0) {
                        Ordering::Greater => {
                            let utf16_slice = &uni_chars[..ret as usize];
                            if let Ok(strval) = String::from_utf16(utf16_slice) {
                                self.key_vals.insert((vk, shift_state), strval);
                            }
                            // If the AltGr version of the key has a different string than
                            // the base, then the layout has AltGr. Note that Mozilla also
                            // checks dead keys for change.
                            if has_altgr
                                && !self.has_altgr
                                && self.key_vals.get(&(vk, shift_state))
                                    != self.key_vals.get(&(vk, shift_state & !SHIFT_STATE_ALTGR))
                            {
                                self.has_altgr = true;
                            }
                        }
                        Ordering::Less => {
                            // It's a dead key, press it again to reset the state.
                            self.dead_keys.insert((vk, shift_state));
                            let _ = ToUnicodeEx(
                                vk as UINT,
                                0,
                                key_state.as_ptr(),
                                uni_chars.as_mut_ptr(),
                                uni_chars.len() as _,
                                0,
                                self.hkl,
                            );
                        }
                        _ => (),
                    }
                }
            }
        }
    }
fn load_keyboard_layout ( & mut self ) { unsafe { self . key_vals . clear ( ) ; self . dead_keys . clear ( ) ; self . has_altgr = false ; let mut key_state = [ 0u8 ; 256 ] ; let mut uni_chars = [ 0u16 ; 5 ] ; // Right now, we're only getting the values for base and shifted // variants. Mozilla goes through 16 mod states. for shift_state in 0 .. N_SHIFT_STATE { let has_shift = shift_state & SHIFT_STATE_SHIFT != 0 ; let has_altgr = shift_state & SHIFT_STATE_ALTGR != 0 ; key_state [ VK_SHIFT as usize ] = if has_shift { 0x80 } else { 0 } ; key_state [ VK_CONTROL as usize ] = if has_altgr { 0x80 } else { 0 } ; key_state [ VK_LCONTROL as usize ] = if has_altgr { 0x80 } else { 0 } ; key_state [ VK_MENU as usize ] = if has_altgr { 0x80 } else { 0 } ; key_state [ VK_RMENU as usize ] = if has_altgr { 0x80 } else { 0 } ; for vk in PRINTABLE_VKS . iter ( ) . cloned ( ) . flatten ( ) { let ret = ToUnicodeEx ( vk as UINT , 0 , key_state . as_ptr ( ) , uni_chars . as_mut_ptr ( ) , uni_chars . len ( ) as _ , 0 , self . hkl , ) ; match ret . cmp ( & 0 ) { Ordering :: Greater => { let utf16_slice = & uni_chars [ .. ret as usize ] ; if let Ok ( strval ) = String :: from_utf16 ( utf16_slice ) { self . key_vals . insert ( ( vk , shift_state ) , strval ) ; } // If the AltGr version of the key has a different string than // the base, then the layout has AltGr. Note that Mozilla also // checks dead keys for change. if has_altgr && ! self . has_altgr && self . key_vals . get ( & ( vk , shift_state ) ) != self . key_vals . get ( & ( vk , shift_state & ! SHIFT_STATE_ALTGR ) ) { self . has_altgr = true ; } } Ordering :: Less => { // It's a dead key, press it again to reset the state. self . dead_keys . insert ( ( vk , shift_state ) ) ; let _ = ToUnicodeEx ( vk as UINT , 0 , key_state . as_ptr ( ) , uni_chars . as_mut_ptr ( ) , uni_chars . len ( ) as _ , 0 , self . hkl , ) ; } _ => ( ) , } } } } }
Load a keyboard layout.
Load a keyboard layout .
&mut self
&mut self