Skip to content

Commit

Permalink
Merge pull request #403 from nickjhughes/press-key-with-modifiers
Browse files Browse the repository at this point in the history
Add `press_key_with_modifiers`
  • Loading branch information
mdrokz committed Sep 28, 2023
2 parents e8f7b6b + b3f137e commit 7241c6d
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/browser/tab/mod.rs
Expand Up @@ -62,6 +62,14 @@ pub mod element;
mod keys;
pub mod point;

#[derive(Debug, Copy, Clone)]
pub enum ModifierKey {
Alt = 1,
Ctrl = 2,
Meta = 4, // Meta/Command
Shift = 8,
}

#[derive(Debug)]
pub enum RequestPausedDecision {
Fulfill(FulfillRequest),
Expand Down Expand Up @@ -868,7 +876,11 @@ impl Tab {
Ok(self)
}

pub fn press_key(&self, key: &str) -> Result<&Self> {
pub fn press_key_with_modifiers(
&self,
key: &str,
modifiers: Option<&[ModifierKey]>,
) -> Result<&Self> {
// See https://github.com/GoogleChrome/puppeteer/blob/62da2366c65b335751896afbb0206f23c61436f1/lib/Input.js#L114-L115
let definiton = keys::get_key_definition(key)?;

Expand All @@ -893,6 +905,8 @@ impl Tab {
let key = Some(definiton.key.to_string());
let code = Some(definiton.code.to_string());

let modifiers = modifiers.map(|v| v.iter().fold(0, |acc, e| acc | *e as u32));

self.optional_slow_motion_sleep(25);

self.call_method(Input::DispatchKeyEvent {
Expand All @@ -902,7 +916,7 @@ impl Tab {
code: code.clone(),
windows_virtual_key_code: Some(definiton.key_code),
native_virtual_key_code: Some(definiton.key_code),
modifiers: None,
modifiers: modifiers.clone(),
timestamp: None,
unmodified_text: None,
key_identifier: None,
Expand All @@ -919,7 +933,7 @@ impl Tab {
code,
windows_virtual_key_code: Some(definiton.key_code),
native_virtual_key_code: Some(definiton.key_code),
modifiers: None,
modifiers,
timestamp: None,
unmodified_text: None,
key_identifier: None,
Expand All @@ -932,6 +946,10 @@ impl Tab {
Ok(self)
}

pub fn press_key(&self, key: &str) -> Result<&Self> {
self.press_key_with_modifiers(key, None)
}

/// Moves the mouse to this point (dispatches a mouseMoved event)
pub fn move_mouse_to_point(&self, point: Point) -> Result<&Self> {
if point.x == 0.0 && point.y == 0.0 {
Expand Down

0 comments on commit 7241c6d

Please sign in to comment.