Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSReadline breaks transparency keyboard shortcuts on Windows 10 console host #280

Closed
pcgeek86 opened this issue Sep 1, 2015 · 6 comments

Comments

@pcgeek86
Copy link

pcgeek86 commented Sep 1, 2015

The {CTRL} + {SHIFT} + {+/-} keyboard shortcuts for the Windows 10 console host don't work with PSReadline imported into the PowerShell session. Removing the module fixes the shortcuts, as a temporary workaround.

Cheers,
Trevor Sullivan
Microsoft MVP PowerShell
http://trevorsullivan.net
http://twitter.com/pcgeek86

@lzybkr
Copy link
Member

lzybkr commented Sep 2, 2015

There's no good fix for this issue that I'm aware of. conhost can't take over those key bindings because applications may use them for their own purposes, but conhost doesn't provide any api for an application (like PSReadline) to access that functionality. This affects some other new and useful key bindings as well, e.g. Ctrl+m for selection.

The conhost team is aware of the issue.

I'll leave this open because there is a hack that I think might work - basically using something like SendKeys and then calling the cooked mode readline to get conhost to process the key. That might work for single keys, but I'm not sure that would work with Ctrl+m because I don't know how to get control back after the selection has been completed.

@mklement0
Copy link

mklement0 commented Nov 24, 2018

Here are workarounds for most of the currently unsupported shortcuts:

Set-PSReadlineKeyHandler Alt+F4 -ScriptBlock { 
  (New-Object -ComObject WScript.Shell).SendKeys('%{F4}')
}

Set-PSReadlineKeyHandler Ctrl+f -ScriptBlock { 
  (New-Object -ComObject WScript.Shell).SendKeys('^f')
}

Set-PSReadlineKeyHandler Ctrl+m -ScriptBlock { 
  (New-Object -ComObject WScript.Shell).SendKeys('^m')
}

Set-PSReadlineKeyHandler F11 -ScriptBlock { 
  (New-Object -ComObject WScript.Shell).SendKeys('{F11}')
}

Set-PSReadlineKeyHandler Ctrl+a -ScriptBlock { 
  $length = 0; $line = ''
  [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref] $line, [ref] $null)
  [Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref] $null, [ref] $length)
  if ($length -eq $line.Length) { # whole line already selected -> select whole scroll-back buffer
    (New-Object -ComObject WScript.Shell).SendKeys('^a')
  } else { # select whole line
    [Microsoft.PowerShell.PSConsoleReadLine]::SelectAll()
  }
}

Given that PowerShell doesn't seem to release references to COM objects, more work may be needed. [edited by @daxian-dbw, the referenced issues was closed]

The transparency-increasing/decreasing shortcuts cannot be emulated this way, because the modifier-key status is lost after the initial keypress, so that auto-repeating invocation for gradual adjustment doesn't work, making the emulation too cumbersome to use.

However, you can at least get the functionality via different shortcut keys, as long as they don't involve modifiers; the following definitions demonstrate that with F9 and F10:

# Decrease transparency
# !! Only works with auto-repeating invocations with a shortcut *without modifiers*,
# !! because the modifier status is lost after the first .SendKeys() call.
# !! Therefore, the original shortcut, Ctrl+_ (Ctrl+Shift+Minus) cannot be used.
Set-PSReadlineKeyHandler F9 -ScriptBlock { 
  (New-Object -ComObject WScript.Shell).SendKeys('^+-')
}

# Increase transparency
# !! Only works with auto-repeating invocations with a shortcut *without modifiers*,
# !! because the modifier status is lost after the first .SendKeys() call.
# !! Therefore, the original shortcut, Ctrl++ (Ctrl+Shift+Plus) cannot be used.
Set-PSReadlineKeyHandler F10 -ScriptBlock { 
  (New-Object -ComObject WScript.Shell).SendKeys('^+{+}')
}

@SteveL-MSFT
Copy link
Member

Should we bind these by default on Windows?

@lzybkr
Copy link
Member

lzybkr commented Jan 13, 2020

Using SendKeys by default seems a bit risky, e.g. if it's a bit slow and you switch to another app (intentionally or not, say another app takes focus), that app will receive the key(s).

@SteveL-MSFT
Copy link
Member

@lzybkr that's true since SendKeys sends it to the OS and not a specific app. Windows Terminal doesn't have this issue, so perhaps it's just documentation

@daxian-dbw
Copy link
Member

daxian-dbw commented Apr 6, 2022

Note that Ctrl+Shift + "mouse wheel scrolling" works with PSReadLine. It's a workaround for the reported issue.
Given that there is nothing much can be done in PSReadLine about this, I will close this issue as external.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants