- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于这个问题,我有一个不太优雅的解决方法,我希望其他人可能已经有了更强大的解决方案。
<小时/>在触摸屏上,点击可编辑文本字段将弹出屏幕键盘,这将更改可用的屏幕空间量。如果不加以处理,这可能会隐藏关键元素,或将页脚推到适当的位置。
在笔记本电脑或台式计算机上,打开可编辑文本字段不会造成此类布局更改。
在我当前的项目中,我希望确保即使虚拟键盘打开时某些关键项目也可见,因此我需要检测何时发生此类更改。然后,我可以向 body
元素添加一个类,以更改布局以适应键盘的存在。
在网上搜索现有的解决方案时,我发现:
contentEditable
元素将打开屏幕键盘我已在下面发布了我提出的解决方案。它依赖于检测键盘焦点变化后一秒内窗口高度的变化。我希望您能提出更好的解决方案,并经过跨平台、跨浏览器和跨设备的测试。
<小时/>我创建了一个 repository on GitHub .
您可以测试我的解决方案here .
在我的测试中,如果用户使用带有触摸屏、键盘和鼠标的计算机,并且首先使用鼠标(取消)选择可编辑元素,然后立即更改窗口高度,则可能会出现误报。如果您在计算机或移动设备上发现其他误报或漏报,请告诉我。
<小时/>;(function (){
class Keyboard {
constructor () {
this.screenWidth = screen.width // detect orientation
this.windowHeight = window.innerHeight // detect keyboard change
this.listeners = {
resize: []
, keyboardchange: []
, focuschange: []
}
this.isTouchScreen = 'ontouchstart' in document.documentElement
this.focusElement = null
this.changeFocusTime = new Date().getTime()
this.focusDelay = 1000 // at least 600 ms is required
let focuschange = this.focuschange.bind(this)
document.addEventListener("focus", focuschange, true)
document.addEventListener("blur", focuschange, true)
window.onresize = this.resizeWindow.bind(this)
}
focuschange(event) {
let target = event.target
let elementType = null
let checkType = false
let checkEnabled = false
let checkEditable = true
if (event.type === "focus") {
elementType = target.nodeName
this.focusElement = target
switch (elementType) {
case "INPUT":
checkType = true
case "TEXTAREA":
checkEditable = false
checkEnabled = true
break
}
if (checkType) {
let type = target.type
switch (type) {
case "color":
case "checkbox":
case "radio":
case "date":
case "file":
case "month":
case "time":
this.focusElement = null
checkEnabled = false
default:
elementType += "[type=" + type +"]"
}
}
if (checkEnabled) {
if (target.disabled) {
elementType += " (disabled)"
this.focusElement = null
}
}
if (checkEditable) {
if (!target.contentEditable) {
elementType = null
this.focusElement = null
}
}
} else {
this.focusElement = null
}
this.changeFocusTime = new Date().getTime()
this.listeners.focuschange.forEach(listener => {
listener(this.focusElement, elementType)
})
}
resizeWindow() {
let screenWidth = screen.width;
let windowHeight = window.innerHeight
let dimensions = {
width: innerWidth
, height: windowHeight
}
let orientation = (screenWidth > screen.height)
? "landscape"
: "portrait"
let focusAge = new Date().getTime() - this.changeFocusTime
let closed = !this.focusElement
&& (focusAge < this.focusDelay)
&& (this.windowHeight < windowHeight)
let opened = this.focusElement
&& (focusAge < this.focusDelay)
&& (this.windowHeight > windowHeight)
if ((this.screenWidth === screenWidth) && this.isTouchScreen) {
// No change of orientation
// opened or closed can only be true if height has changed.
//
// Edge case
// * Will give a false positive for keyboard change.
// * The user has a tablet computer with both screen and
// keyboard, and has just clicked into or out of an
// editable area, and also changed the window height in
// the appropriate direction, all with the mouse.
if (opened) {
this.keyboardchange("shown", dimensions)
} else if (closed) {
this.keyboardchange("hidden", dimensions)
} else {
// Assume this is a desktop touchscreen computer with
// resizable windows
this.resize(dimensions, orientation)
}
} else {
// Orientation has changed
this.resize(dimensions, orientation)
}
this.windowHeight = windowHeight
this.screenWidth = screenWidth
}
keyboardchange(change, dimensions) {
this.listeners.keyboardchange.forEach(listener => {
listener(change, dimensions)
})
}
resize(dimensions, orientation) {
this.listeners.resize.forEach(listener => {
listener(dimensions, orientation)
})
}
addEventListener(eventName, listener) {
// log("*addEventListener " + eventName)
let listeners = this.listeners[eventName] || []
if (listeners.indexOf(listener) < 0) {
listeners.push(listener)
}
}
removeEventListener(eventName, listener) {
let listeners = this.listeners[eventName] || []
let index = listeners.indexOf(listener)
if (index < 0) {
} else {
listeners.slice(index, 1)
}
}
}
window.keyboard = new Keyboard()
})()
最佳答案
有一个新的实验性 API,旨在准确跟踪由于键盘出现和其他类似的移动怪异而导致的尺寸变化。
window.visualViewport
https://developer.mozilla.org/en-US/docs/Web/API/Visual_Viewport_API
通过监听调整大小事件并将高度与所谓的“布局视口(viewport)”的高度进行比较。看到它发生了很大的变化,比如大约 30 像素。您可能会推断出类似“键盘正在显示”之类的信息。
if('visualViewport' in window) {
window.visualViewport.addEventListener('resize', function(event) {
if(event.target.height + 30 < document.scrollElement.clientHeight) {
console.log("keyboard up?");
} else {
console.log("keyboard down?");
}
});
}
(上面的代码未经测试,我怀疑缩放可能会触发误报,可能还需要检查缩放更改)
关于javascript - 检测触摸屏设备上虚拟键盘的打开或关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47841986/
我遇到了一个问题。我正在使用 pyinstaller 将代码导出到 .exe。代码包括 tkinter、PIL 和 keyboard 模块。软件在我使用 Python 的 PC 上运行完美,而在没有
我正在尝试将 STM32F0-disco 用作 Windows PC 的键盘。正在打印的字符有问题。 下面的代码等到板载按钮被按下,然后应该打印一次这三个字符。 /* USER CODE BE
我想在单击键盘上的“完成”按钮时退出键盘。我怎样才能做到这一点?我有以下代码=> textView.returnKeyType = UIReturnKeyDone; 我有这个代码用于限制 TextVi
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我想调出一个用于输入电话号码的键盘。有没有办法在我自己的 Activity 中显示此对话框并捕获用户输入的内容。我需要能够控制用户点击调用时发生的情况。所以我很感兴趣自定义允许用户输入电话号码的 Ac
我希望软键盘位于我的布局之上,而不是在它弹出时四处移动我的布局。我该怎么做呢?我查看了 list ,但从未设置 android:windowSoftInputMode="adjustResize" 此
主题:将某些键替换为另一个键值。 例如如果我按 P,它应该是 F24。 当我尝试从 .ini 文件加载键值时, Hook 不再是全局的。它仅在 winapi 窗体处于焦点时才有效。 我的 DLL 代码
当我点击 EditText 时,是否可以用小写字母启动 android 键盘?基本上,我想要的是将 EditText 中的第一个字母小写,但也让用户可以根据需要将其变为大写...... (EditTe
我想监控 IOS 上的键盘隐藏按钮并触发一个事件。我在说: 我只想在用户按下实际按钮时进行监控。我不想要键盘隐藏时的事件。 最佳答案 用户键盘通知观察者.. 对于 swift NSNotificati
在经历了这一切之后就像认真的...... Easy way to dismiss keyboard? ...我有多个 TextFields 和一些 TextViews。有没有办法对所有文本字段进行批处
所以我想在我的应用程序中添加一个键盘,其中包含表情符号,就像 Whatsapp 或环聊一样。我怎样才能做到这一点?我想保留我的键盘,因为我只想添加标签来放置表情符号。我认为软键盘很容易支持它,但到目前
首先,我会让您知道,我在 StackOverflow 以及 Google 和 Github 上查看了很多很多不同的帖子。我已经到处寻找对我有帮助的任何。但是,似乎没有任何效果。它要么已经过时(超过 1
当按下相应的键时,我试图让我的游戏中的宇宙飞船 (PlayerShip.gif) 向左、向右、向上和向下移动。我知道我需要一个 keyboardListener 但我在弄清楚它的去向以及它是如何实际实
我正在尝试制作一个 HID USB 设备。我搜索了一下,发现键盘的输出有 8 个字节。第一个字节是修饰符,第二个字节是保留字节,其余 6 个字节是关键代码。我认为在某些情况下,例如“prtsc”,需要
我最近开始学习 Vim,在深入学习之前,我有一个问题需要回答。 使用 AZERTY 键盘,我是否应该重新映射命令和快捷方式的键以适应 QWERTY 键盘的键位置? 我之所以问这个,是因为显然在创建这些
我一直认为在使用 Dvorak 布局之前,我需要购买 Dvorak 键盘。但是我在亚马逊上找不到。仅仅是从 Qwerty 键盘上弹出键并移动它们吗? 最佳答案 为了帮助您了解键盘布局,您可以重新排列
我不敢相信我还没有找到任何关于此的文档,但我想知道如何命令键盘激活并接收来自它的输入。我可以找到在编辑文本字段时操作弹出键盘的所有示例。谢谢 最佳答案 您还可以使用 UIKeyInput 协议(pro
我有一个 UITextField,其中弹出的键盘已禁用其 Shift 键。键盘类型设置为 UIKeyboardTypeNamePhonePad,看起来应该允许大写。 如果我将键盘类型更改为“默认”但保
背景:我的表单有一个 TWebBrowser。我想用 ESC 关闭表单,但 TWebBrowser 吃掉了击键 - 所以我决定使用键盘 Hook 。 问题是表单可以同时在多个实例中打开。 无论我做什么
我需要(即客户要求)提供自定义键盘,供用户在文本字段和区域中输入文本。我已经有一些可以执行键盘操作并将测试附加到文本字段的东西,但是我想让它更通用并让它像标准的 iphone 键盘一样工作,即当用户选
我是一名优秀的程序员,十分优秀!